Novice regexes

Regular expressions allow you to form sets of words using 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:

Regex

String

Result

/[Ff]oo/

Foo

Match

/[^Ff]oo/

Foo

No match; the regex 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 learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Advanced regexes >>

Previous chapter: Basic regexes with preg_match() and preg_match_all()

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.