XQuery adds the concept of anchors to XML Schema regular expressions. In XML Schema validation, the regular expression is expected to match the entire string, not a part of it. For example, the regular expression str matches only the string str and not other strings that contain str, like 5str5. In XQuery, however, the expression str matches all strings that contain str, including 5str5.

Because of this looser interpretation, it is sometimes useful to explicitly say that the expression should match the beginning or end of the string (or both). Anchors can be used for this purpose. The ^ character is used to match the beginning of the string, and the $ character is used to match the end of the string. For example, the regular expression ^str specifies that a matching string must start with str. Table 18-13 shows some examples that use anchors.

Table 18-13. Anchors

Regular expression

Strings that match

Strings that do not match


str, str5, 5str, 5str5

st, sttr



5str5, str5, 5str


str, str5

5str5, 5str


str, 5str

5str5, str5

Anchors and Multi-Line Mode

Some XQuery functions (namely matches, replace, and tokenize) allow you to indicate that the processor should operate in multi-line mode. This is specified using the letter m in the $flags argument. In multi-line mode, anchors match not just the beginning and end of the entire string, but also the beginning and end of any line within the string, as indicated by a line ...

Get XQuery now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.