Reluctant Quantifiers
XQuery supports reluctant quantifiers,
which allow part of a regular expression to match the shortest possible string. Reluctant quantifiers are indicated by adding a question mark (?) to the end of any of the kinds of quantifiers identified in Table 18-1.
For example, given the string reluctant and the regular expression r.*t, the regular expression could match reluct or reluctant. Since a standard quantifier (*) is used, the match is on the longest possible string, reluctant. If the regular expression were r.*?t instead, which uses a reluctant quantifier, it would match reluct, the shorter of the two strings.
Reluctant quantifiers come into play when replacing matching values in a string. Table 18-12 shows some examples of calls to the replace function that use reluctant and nonreluctant quantifiers.
Table 18-12. Reluctant versus nonreluctant quantifiers
|
Example |
Return value |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Reluctant quantifiers have no effect on simply determining whether a string matches a regular expression, which explains why they are not supported in XML Schema. It may seem that the regular expression r.*?tly would not match the string reluctantly because r.*?t would match the shorter string reluct, leaving an extra antly which does ...
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