XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 1491915103

2nd edition, , O'Reilly Media, Inc.

Chapter 19: Regular Expressions

Please note that the book contains many inline examples and informal tables that are not provided here.

Table 19-2. Quantifier examples
Regular expressionStrings that matchStrings that do not match
fo fo f, foo
fo? f, fo foo
fo* f, fo, foo, fooo, ... fx
fo+ fo, foo, fooo, ... f
fo{2} foo fo, fooo
fo{2,} foo, fooo, foooo, ... f, fo
fo{2,3} foo, fooo f, fo, foooo
Table 19-3. Examples of parentheses in regular expressions
Regular expressionStrings that matchStrings that do not match
(fo)+z foz, fofoz z, fz, fooz, ffooz
(fo|xy)z foz, xyz z
(fo|xy)+z fofoz, foxyz, xyfoz z
(f+o)+z foz, ffoz, foffoz z, fz, fooz
yes|no yes, no yeno
Table 19-5. Representing individual characters
Regular expressionStrings that matchStrings that do not match
d d g
d+efg+ defg, ddefgg defgefg, deffgg
defg defg d, efg
d|e|f d, e, f g
f*o fo, ffo, fffo f*o
f\*o f*o fo, ffo, fffo
déf déf def, df
Table 19-6. The wildcard escape character
Regular expressionStrings that matchStrings that do not match
f.o fao, fbo, f2o fo, fbbo
f..o faao, fbco, f12o fo, fao
f.*o fo, fao, fbcde23o f o
f\.o f.o fao
Table 19-10. Representing groups of characters
Regular expressionStrings that matchStrings that do not matchComment
f\d f0, f1 f, f01 Multi-character escape
f\d* f, f0, f012 ff Multi-character escape
f\s*o fo, f o foo Multi-character escape
\p{Ll} a, b A, B, 1, 2 Category escape
\P{Ll} A, B, 1, 2 a, b Category escape
\p{L} a, b, A, B 1, 2 Category escape
\P{L} 1, 2 a, b, A, B Category escape
\p{IsBasicLatin} a, b â, ß Block escape
\P{IsBasicLatin} â, ß a, b Block escape
Table 19-11. Character class expression examples
Regular expressionStrings that matchStrings that do not matchComment
[def] d, e, f def Single characters
[def]* d, eee, dfed a, b Single characters, repeating
[\p{Ll}\d] a, b, 1 A, B Single characters with escapes
[d-f] d, e, f a, D Range of characters
[0-9d-fD-F] 3, d, F a, 3dF Multiple ranges
[0-9stu] 4, 9, t a, 4t Range plus single characters
[s-u\d] 4, 9, t a, t4 Range plus single-character escape
[a-x-[f]] a, d, x f, 2 Subtracting from a range
[a-x-[fg]] a, d, x f, g, 2 Subtracting from a range
[a-x-[e-g]] a, d, x e, g, 2 Subtracting from a range with a range
[^def] a, g, 2 d, e, f Negating single characters
[^\[] a, b, c [ Negating a single-character escape
[^\d] d, E 1, 2, 3 Negating a multi-character escape
[^a-cj-l] d, 4 b, j, l Negating a range
Table 19-12. Reluctant versus non-reluctant quantifiers
ExampleReturn 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
Table 19-13. Using anchors
ExampleReturn value
matches("string", "^str") true
matches("string", "^ing") false
matches("string", "str$") false
matches("string", "ing$") true
matches("string", "^s.*g$") true
matches("string", "rin") true
replace("aaaha", "^a", "X") Xaaha
replace("aaaha", "^a+", "X") Xha
replace("aaaha", "a", "X") XXXhX
replace("aaaha", "a$", "X") aaahX
Table 19-14. Examples of the $flags argument
ExampleReturn value
matches($address, "Street.*City") false
matches($address, "Street.*City", "s") true
matches($address, "Street$") false
matches($address, "Street$", "m") true
matches($address, "street") false
matches($address, "street", "i") true
matches($address, "Main Street") true
matches($address, "Main Street", "x") false
matches($address, "Main \s Street", "x") true
matches($address, "[0-9]+") true
matches($address, "[0-9]+", "q") false
matches($address, "street$", "im") true
Table 19-15. Examples of using replacement variables
ExampleReturn value
replace("Chap 2...Chap 3...Chap 4...", "Chap (\d)", "Sec $1.0") Sec 2.0...Sec 3.0...Sec 4.0...
replace("abc123", "([a-z])", "$1x") axbxcx123
replace("2315551212", "(\d{3})(\d{3})(\d{4})", "($1) $2-$3") (231) 555-1212
replace("2015-10-18", "\d{2}(\d{2})-(\d{2})-(\d{2})", "$2/$3/$1") 10/18/15
replace("25", "(\d+)", "\$$1.00") $25.00
Datypic XQuery Services