▼  Site Navigation Main Articles Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc News Links to Webcomics Bible Quotes About Older Musings
site version 7.3
PCRE –– tutorial –– supplemental
written by: admin


Date Written: 6/8/10 Last Updated: 6/16/10

intro
In this tuorial we begin to explore some of the more complicated aspects of PCRE.  Many people who dabble in PCRE are unfamiliar with these.  They are not well documented either, so I will try to explore most of them here with examples and detailed explanations.  Assertion declarations do not consume characters.


Lesser known Assertion types
\b = boundary between a word and a non–word.
\B = any boundary that is not a boundary between word characters and non word characters.
\A = start of subject (independent of multiline mode)
\Z = end of subject or newline at end (independent of multiline mode)
\z = end of subject (independent of multiline mode)
\G = first matching position in subject. used with preg_match()
\Q = ignore regex metacharacters
\E = ignore regex metacharacters
\K = reset the match start

$string='this is fine.';
$string=preg_replace('/\B/','X',$string);
print $string;

The above code will produce:
tXhXiXs iXs fXiXnXe.X
\B is a boundary character, so it looks for two characters next to each other and detects the relationship between the two.  For example in the string "this is fine." listed above PCRE will first look to see how the characters t and h relate to each other.  They are both word characters, so \B says that there is no boundary between a word character and a non word character therefore the match is valid and an "X" is placed between the two letters.  The PCRE goes on to check "hi" to examine the relationship between the two and then "is".  This continues till we get to "s "  since the letter "s" is next to a whitespace character there is a boundary between word character and a non word character, therefore the \B fails and "X" is not inserted between s and the whitespace.  Again, \B matches the boundary between two characters that is not a word character next to a non–word character.


some helpful links


TAGS: php, pcre
Comments

Go to phpbb thread.
copyright 2005–2021