[XPath] Reluctant Quantifiers
By default, a regular expression or subexpression matches
the longest possible string. The quantifiers +, *, and
? match as many characters as
possible. Adding a question mark to the end of a quantifier causes the
expression or subexpression to match the shortest
possible string. The quantifiers are modified as follows:
a+?Matches
aoncea??Matches
a, either once or not at all (works only in thematches()function; more on this later in this section)a*?Matches
a, zero times or once (works only in thematches()function; more on this later in this section)a{x}?Matches
a, exactlyxtimes (in this case the reluctant qualifier doesn’t change anything)a{x,}?Matches
aexactlyxtimes (the comma is irrelevant—a reluctant quantifier matches only the minimum length)a{x,y}?Matches
aexactlyxtimes (the second number,y, is irrelevant—a reluctant quantifier matches only the minimum length)
As an example, we’ll use the replace() function. We’ll put square brackets around each match in the
original string. The normal quantifier and the reluctant quantifier work
differently, as we’ll see. Here’s the stylesheet:
<?xml version="1.0" encoding="utf-8"?> <!-- reluctant.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>Original string: 'Call me at 19195551212'
</xsl:text> <xsl:text> replace($x, '([0-9]+)', '[$1]'):
 </xsl:text> <xsl:value-of select="replace('Call ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access