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 not match the pattern ly
. However, this is not the way it works. Reluctant quantifiers do not indicate that only the shorter string matches, just that the processor uses the shorter of the two matches if called on to perform a replacement or some other operation. Any of the quantifiers in the examples in Table 18-2 could be replaced by reluctant quantifiers, and the list of matching and nonmatching strings would be the same.
18.117.231.15