O'Reilly logo

XQuery by Priscilla Walmsley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Anchors

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

str, str5, 5str, 5str5

st, sttr

^str$

str

5str5, str5, 5str

^str

str, str5

5str5, 5str

str$

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required