XML technologies / Regular expressions / Practical regex techniques / HTML examples / Matching HTML tags
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The regular expression ˹<[^>]+>˼ is often used to match an HTML tag. It usually works, for example in this Perl fragment:
$html =~ s/<[^>]+>//g;However, a problem arises when the tag contains a ›>‹, as in this perfectly valid HTML tag: <img src=rechtspfeil.png alt=">">. This is uncommon and not recommended, but a tag attribute may certainly contain ›<‹ or ›>‹ provided they are quoted. Our simple ˹<[^>]+>˼ does not account for this, so we must make it smarter.
Within ›<...>‹ quoted strings are allowed, as is “other stuff” that need not be quoted: anything except ›>‹ and quotation marks. HTML allows either double or single quotes as delimiters, but neither can be escaped with a backslash. We can therefore use relatively simple regular expressions such as ˹"[^"]*"˼ and ˹'[^']*'˼.
Together with the regex ˹[^'">]˼ for the “other stuff”, this gives:
˹<("[^"]*"|'[^']*'|[^'">])*>˼
This is rather confusing, so here it is again in free-spacing mode:
< # Öffnendes "<"
( # Jede Anzahl von ...
"[^"]*" # Strings in Anführungszeichen
| # oder ...
'[^']*' # Strings in Hochkommas
| # oder ...
[^'">] # "anderes Zeug".
)* #
> # Schließendes ">"This approach is quite elegant. Each quoted string is treated as a unit, clearly specifying which characters are allowed where. Each character can match only one alternative, so there is no ambiguity and no danger of unwanted matches slipping in as in earlier examples.
The first two alternatives use ˹*˼ rather than ˹+˼ because a single- or double-quoted string may be empty (for example, ›alt=""‹). In contrast, the third alternative must contain neither ˹*˼ nor ˹+˼ because a quantifier already applies to the group enclosing the alternation. An additional quantifier, such as ˹([^'">]+)*˼, would give us an unpleasant surprise, discussed in detail in The art of writing regular expressions (see Allowing greediness only locally).
A few more thoughts on NFA efficiency: if we do not need the text captured by the parentheses, we can replace them with noncapturing groups (see Grouping-only (noncapturing) parentheses). Because the alternatives are unambiguous, retrying the others is pointless when the final ˹>˼ is not found. If one alternative matched at a position, none of the others can match there. We can therefore safely discard the saved states: they would only lead to failures anyway. Thus we can also replace the noncapturing groups with ˹(?>...)˼, that is, atomic groups (or quantify them with a possessive star).
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