Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #10626
| From | Fiver <numeralfive@gmail.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: CTYPE |
| Date | 2013-08-25 22:42 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <kvdq7d$di5$1@dont-email.me> (permalink) |
| References | <kvdg6l$cni$1@speranza.aioe.org> <b7v18bFpg13U1@mid.individual.net> <kvdlrv$si2$1@speranza.aioe.org> |
On 2013-08-25 21:28, Twayne wrote:
> On 2013-08-25 2:38 PM, J.O. Aho wrote:
>> You know you will end up with loads of if cases if you keep on doing
>> this instead of using a regular expression, this would speed it up and
>> make your code a lot easier to read (case insensitive):
>>
>> if(preg_match("/^[a-z]\d[a-z] \d[a-z]\d$/i",$postcode)) {
>> // this could be a valid Canadian postcode
>> // just add what you need to do to check it against
>> // Canadian Post, worst case use the "Find a post office"
>> }
> I agree with you but I'm not useing regular expressions. 1 they're never
> clear to me, 2 I'm trying to avoid them as I have a couple in other
> cases where they actually cause noticeable slow-downs on even my Local
> Server, and magnitudes worse with I upload them to be "live", 3 there
> are so many ways to do the same thing in PHP my goal is to learn them
> and take notes of the most useful.
> So it's not just learning to code properly, it's also to learn
> better, less-code ways of doing things.
Your original question was answered, but it seems you have a few
misconceptions about regular expressions. If you into learning how to do
things properly, you shouldn't dismiss them out of hand.
ad 1) "they're never clear to me"
That's a fair point, as regexes can quickly become hard to read. There's
a way around that, however: the /x modifier. Take for example the regex
in the post you replied to (just as an example, I'm not judging its
correctness for now):
preg_match("/^[a-z]\d[a-z] \d[a-z]\d$/i", $postcode)
This can also be written as
$regex = '/
^ # anchored at the start of the string
[a-z] # one letter a-z
\d # one digit 0-9
[a-z] # one letter a-z
\s # one character of white space *
\d # one digit 0-9
[a-z] # one letter a-z
\d # one digit 0-9
$ # anchored at the end of the string
/ix';
preg_match($regex, $postcode)
Much easier to understand this way, especially if you're starting out
with regular expressions. That's all one string - all the white space
and the comments are ignored. (Talking about white space, a more exact
way to match "one space character" would be "[ ]").
ad 2) "they're too slow"
That may have been true in other languages and many, many years ago, but
it's certainly not the case with PHP today. This regex runs over 2
million times per second on a single core of my middle-aged laptop. It
should be more than fast enough, considering I could check every
Canadian's zip code in under 15 seconds with it... I don't know what
you're building, but I can guarantee that this won't be the reason for
any noticeable slow-downs.
ad 3) "there are many other ways"
True, but the regex is by far the shortest. It's also what most
programmers would expect to see in this situation. It is, as you say, a
"better, less-code" way to do it.
[snip]
> I dislike saying all those things about myself
I found it interesting to read.
regards,
5er
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
CTYPE Twayne <nobody@spamcop.net> - 2013-08-25 13:51 -0400
Re: CTYPE Richard Yates <richard@yatesguitar.com> - 2013-08-25 11:21 -0700
Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-25 15:00 -0400
Re: CTYPE Richard Yates <richard@yatesguitar.com> - 2013-08-25 12:05 -0700
Re: CTYPE "J.O. Aho" <user@example.net> - 2013-08-25 20:38 +0200
Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-25 15:28 -0400
Re: CTYPE Richard Yates <richard@yatesguitar.com> - 2013-08-25 13:16 -0700
Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-26 11:49 -0400
Re: CTYPE Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-25 22:19 +0200
Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-26 12:15 -0400
Re: CTYPE Fiver <numeralfive@gmail.com> - 2013-08-25 22:42 +0200
Re: CTYPE Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-26 10:25 +0200
Re: CTYPE The Natural Philosopher <tnp@invalid.invalid> - 2013-08-26 09:29 +0100
Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-26 12:37 -0400
Re: CTYPE Norman Peelman <npeelman@cfl.rr.com> - 2013-08-25 15:11 -0400
Re: CTYPE Tim Streater <timstreater@greenbee.net> - 2013-08-25 23:04 +0100
Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-25 15:35 -0400
Re: CTYPE THREAD RESOLVED Fiver <numeralfive@gmail.com> - 2013-08-26 20:07 +0200
Re: CTYPE THREAD RESOLVED The Natural Philosopher <tnp@invalid.invalid> - 2013-08-26 20:19 +0100
Re: CTYPE THREAD RESOLVED Fiver <numeralfive@gmail.com> - 2013-08-26 22:51 +0200
Re: CTYPE THREAD RESOLVED Jerry Stuckle <jstucklex@attglobal.net> - 2013-08-26 16:57 -0400
Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-27 17:20 -0400
Re: CTYPE THREAD RESOLVED Jerry Stuckle <jstucklex@attglobal.net> - 2013-08-27 19:47 -0400
Re: CTYPE THREAD RESOLVED Tim Streater <timstreater@greenbee.net> - 2013-08-26 22:07 +0100
Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-28 08:49 -0400
Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-27 17:18 -0400
csiph-web