Regular expressions

So far we've been looking at string functions to help you manipulate strings, but what if you want more power - more control over your string manipulation?

Regular expressions, usually referred to as regexes, offer you that power, but are tricky to learn because they use complicated syntax to provide their power. The chances are that you have used regular expressions before, albeit perhaps on a fairly limited scale. For example, if you were at your shell prompt and typed "rm *.txt", you would expect all files ending with .txt to be removed.

Similarly, if you were to search your hard drive for all JPEG picture files, you would search for *.jpg - it would match foo.jpg, bar.jpg, fool.jpg, and barf.jpg. You've probably also used the ? regex, which means "match precisely one character" - foo?.jpg would match only fool.jpg and not foobar.jpg.

Regular expressions offer a powerful and easy-to-use (if tricky to learn) way to perform powerful string matching and replacement functions. Regexes can:

  • Replace text

  • Test for a pattern within a string

  • Extract a substring from within a string

We'll be looking at all three of these uses in this section, as well as providing a comprehensive list of the different expressions you can use to work with all kinds of strings.

Before we start, I want to make it clear that you should only use regular expressions when you have to. The set of string functions we just looked at are much faster, much easier to read, and much less hassle to use.

 

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: Basic regexes with preg_match() and preg_match_all() >>

Previous chapter: Parsing a string into variables

Jump to:

 

Home: Table of Contents

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