NYCPHP Meetup

NYPHP.org

[nycphp-talk] Question about REGEX's...

Greg Rundlett greg.rundlett at gmail.com
Tue Nov 28 23:02:23 EST 2006


One nice 'trick' in using regex to match code is to use another
delimiter character bessides '/' since it is so commonly found in the
'needle' pattern.  Other 'common' delimiters are % or #.

e.g. instead of
preg_match('/needle/imUs', $haystack);
do this
preg_match('#needle#imUs', $haystack);

Using alternate pattern delimiters means that you do NOT have to
escape '\' any slash '/'
e.g. (a rough untested pattern that matches and captures anchor tag
parts inside div tags)
preg_match('#<div>.*<a href="([^"]+)">([^<]+)</a>\s*</div>#imUs', $haystack);


ps
imUs is my mnemonic for pattern modifiers
i = case insensitive
m = multi-line so ^ and $ pattern anchors can match many times per
file instead of only once
U = ungreedy
s = "dot-all" such that the '.' meta character matches/includes new
line characters.



More information about the talk mailing list