XML technologies / Regular expressions / How regex engines work / More on greediness / Multicharacter “quotes”
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
In Introduction to regular expressions, I briefly discussed HTML tags, specifically the sequence ...<B>sehr</B>..., which a browser displays as bold “sehr”. Searching for a pattern such as <B>...</B> is analogous to the previous approach, except that the quotes now contain multiple characters: <B> and </B>. As with ordinary quotes, problems with ˹.*˼ arise when one line contains several tagged strings, for example:
...<B>Millionen</B> und <B>Abermillionen</B> von Sonnen...
If we use ˹<B>.*</B>˼, the ˹.*˼ runs to the end of the line, and subsequent backtracking returns only to the last </B> rather than the one corresponding to ˹<B>˼ at the start of the match.
Since the closing delimiter has multiple characters, a character class cannot solve this problem. Something as misguided as ˹<B>[^</B>]*</B>˼ cannot work. A character class always matches a single character, never a sequence such as </B> . Do not let the way ˹[^</B>]˼ is written mislead you. It is an ordinary class matching one character that is neither <, >, / nor B. It is exactly the same class as ˹[^/<>B]˼ and certainly cannot express “anything except </B>”. A lookahead can require that no </B> occur from a particular position; you will see this in Lazy quantifiers.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top