4.8.2 Novice regexpsThis is NOT the latest copy of this book; click here for the latest version.
Regular expressions allow you to form sets of words using square brackets [ and ]. For example, I can define a set [Ff] that will match "F" or "f". You can also use sets to accept ranges, for example [A-Z] will accept all uppercase letters, [A-Za-z] will accept all letters whether uppercase or lowercase, and [a-z0-9] will accept lowercase letters and numbers only. Inside sets, the caret symbol ^ means "not", therefore [^A-Z] will accept everything that is not an uppercase letter, and [^A-Za-z0-9] will accept symbols only - no uppercase letters, no lowercase letters, and no numbers.
Here is a list of some novice regular expressions, again along with string used to match, and whether or not a match is made:
|
Regexp
|
String
|
Result
|
|
/[Ff]oo/
|
Foo
|
Match
|
|
/[^Ff]oo/
|
Foo
|
No match; the regexp says "Anything that is not F or f, followed by "oo". This would match "too", "boo", "zoo", etc.
|
|
/[A-Z][0-9]/
|
K9
|
Match
|
|
/[A-S]esting/
|
Testing
|
No match; the acceptable range for the first character ends at S
|
|
/[A-T]esting/
|
Testing
|
Match; the range is inclusive
|
|
/[a-z]esting[0-9][0-9]/
|
TestingAA
|
No match
|
|
/[a-z]esting[0-9][0-9]/
|
testing99
|
Match
|
|
/[a-z]esting[0-9][0-9]/
|
Testing99
|
No match; case sensitivity!
|
|
/[a-z]esting[0-9][0-9]/i
|
Testing99
|
Match; case problems fixed
|
|
/[^a-z]esting/
|
Testing
|
Match; first character can be anything that is not a, b, c, d, e, etc (lowercase)
|
|
/[^a-z]esting/i
|
Testing
|
No match; the range excludes lowercase characters only, so you would think T would be fine. However, the "i" at the end makes it insensitive, which turns [^a-z] into [^a-zA-Z]
|
The last one is a common "gotcha", so make sure you understand why it does not match.
|
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|