Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Regular expressions Wish List | Report Bug | About Me ]

4.8.2     Novice regexps

This 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.





<< 4.8.1 Basic regexps with preg_match() and preg_match_all()   4.8.3 Advanced regexps >>
Table of Contents
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!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
~!~ Ф€&#358;°® ¿Ä¢&#1180;Å&#321; ~!~ - 16 Oct 2008

Nice Explanation
Even though it would be better if you showed a source code that may be more clear for other vistitors



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow