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 feed character (#xA). Table 18-14 shows some examples of using anchors in multi-line mode.

Table 18-14. Anchors in multi-line mode[a]

Regular expression

Strings that match

Strings that do not match

str

str

st

^str$

str,

555str

 

555

555

 

str

 
 

555

 

^str

str555,

555str

 

555

555

 

555str

 

str$

555str,

555

 

555str

str555

 

555

 

[a] Some of the examples span several lines; individual examples are separated by commas.

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

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