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:

^.*news

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:

/^.*news/

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 .

Modifiers

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:

This
is a multi line
string

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

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?