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

replace("reluctant", "r.*t", "X")

X

replace("reluctant", "r.*?t", "X")

Xant

replace("aaah", "a{2,3}", "X")

Xh

replace("aaah", "a{2,3}?", "X")

Xah

replace("aaaah", "a{2,3}", "X")

Xah

replace("aaaah", "a{2,3}?", "X")

XXh

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.129.90