Pattern matching with PHP

Pattern matching with PHP: Part IV

Over the previous pages, we've focused on regular expressions. The implementation in the extension follows the POSIX standard. An alternative and much more feature-rich implementation is found in the Perl scripting language. Perl-compatible regular expressions (PCREs) can be used in PHP, and they're significantly faster and easier to use than the POSIX ones.

Using PCRE

Unlike the POSIX regular expressions we've used over the last two months, patterns in PCREs must be delimited. For example, consider the following pattern:


This matches the start of a line, followed by all characters up until the string 'news'. The same regular expression, used as a PCRE, looks like this:


The start and end of the pattern is delimited with a slash. The slash is the most widely used delimiter, but any other non alpha-numeric character is valid, as are square brackets [], curly brackets {} and less-than and greater-than symbols .


Delimiters are used to separate the pattern from syntax modifiers. Modifiers change how the PCRE library works. Every modifier is represented by a unique character. The modifier is placed at the end of the pattern, after the closing delimiter. For example, to use a modifier 'X', a PCRE would take the following form: '/pattern/X'.

The following problems can be solved with PCRE modifiers:

Case: using the modifier 'i', the PCRE library will ignore case when matching characters in the pattern against the string. For example, the RE '/abc/i' will match 'Abc', 'ABC', 'aBc' and so on.

Multiple lines: unlike POSIX regular expressions, PCREs do not operate on a single line at a time. That is, a string is treated as a single 'line', even if it contains embedded new lines. The '^' meta character matches the start of the string and '$' matches the end of the string. By using the 'm', or multi line, modifier the RE is executed in the same way as a POSIX regular expression. (New lines can be matched with the '\n' meta character.)

Matching new lines: we have already established that the '.' meta character matches every character except a new line. Since, however, PCREs treat a string as a single line this can be inconvenient. Using the 's' modifier, '.' will match all characters. This means that the PCRE '/^.*$/s' will match the whole string:

is a multi line

Greediness: default wild card matching in POSIX REs and PCREs is said to be greedy. That is, the largest pattern possible is matched. For example, the PCRE '/a.*c/' executed on the string 'abcbcdef' will match 'abcbc'. It is greedy because it consumes as many characters as possible. This behaviour can be reversed with the 'U', or ungreedy, modifier. The PCRE '/a.*c/U' matches 'abc'.

Meta characters

The PCRE extension also provides some useful meta characters missing from or which are very hard to use in the POSIX extension. Some the most useful meta characters deal with isolating words in a string.

For example, say you wanted to locate the word in the string 'Hello!?'.

There are three characters which need to be ignored. The following PCRE would be able to locate just the word: '/[^'!?]/+'. There are, however, many other non-alphabetical characters. Instead of forcing users to determine them and then type them out in every pattern, the '\w' meta character can be used to match any character in a word. As such, our RE can be simplified to '/\w+/'.

Complementing this is a meta character to match non-word characters: \W (upper case 'w'). This meta character will match white space (tabs, spaces, and so on), non-alphabetical characters such as punctuation and all those other characters you always forget about.

It is important to be able to differentiate white space from punctuation. To this end, the '\s' meta character can be used to match any white space character. Its complement is '\S' (upper case 's'), which matches any non-white space character (including punctuation).

Finally, the '\b', or word boundary meta character, can be used to match the start or end of a word. This is analogous to '^' and '$' - the start and end of a line.

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

Xiro Drone Xplorer V -3 Axis Gimbal & 1080p Full HD 14MP Camera

Learn more >

Crucial® BX200 SATA 2.5” 7mm (with 9.5mm adapter) Internal Solid State Drive

Learn more >

D-Link TAIPAN AC3200 Ultra Wi-Fi Modem Router (DSL-4320L)

Learn more >

D-Link PowerLine AV2 2000 Gigabit Network Kit

Learn more >

ASUS ROG Swift PG279Q – Reign beyond virtual world

Learn more >

Lexar® Professional 1000x microSDHC™/microSDXC™ UHS-II cards

Learn more >

Gadgets & Things


Learn more >

Lexar® Professional 1000x microSDHC™/microSDXC™ UHS-II cards

Learn more >

Lexar Professional 2000x SDHC™/SDXC™ UHS-II cards

Learn more >

Family Friendly

ASUS VivoPC VM62 - Incredibly Powerful, Unbelievably Small

Learn more >

Lexar® Professional 1000x microSDHC™/microSDXC™ UHS-II cards

Learn more >

Lexar Professional 2000x SDHC™/SDXC™ UHS-II cards

Learn more >

Stocking Stuffer

Lexar Professional 2000x SDHC™/SDXC™ UHS-II cards

Learn more >

Lexar® Professional 1000x microSDHC™/microSDXC™ UHS-II cards

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Best Deals on PC World

Latest News Articles


GGG Evaluation Team

Kathy Cassidy


First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni


For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell


The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi


The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott


My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.


Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?