XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (589 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
4.63Mb size Format: txt, pdf, ePub

Note also that the
i
flag can be used to achieve case-blind matching in regular expressions used by the
matches()
,
replace()
, and
tokenize()
functions.

See Also

translate()
on page 897

upper-case()
on page 910

matches

The
matches()
function tests whether a supplied string matches a regular expression.

Signature

Argument
Type
Meaning
input
xs:string?
The string to be tested against the regular expression. If an empty sequence is supplied, it is treated as a zero-length string.
regex
xs:string
The regular expression.
flags
(optional)
xs:string
One or more letters indicating options on how the matching is to be performed. If this argument is omitted, the effect is the same as supplying a zero-length string, which defaults all the option settings.
Result
xs:boolean?
True if the input string matches the regular expression, false if not
.

Effect

Regular expressions provide a powerful pattern-matching capability for strings.

The syntax of regular expressions supported by this function is described in Chapter 14. The syntax is based on the regular expression syntax defined for the pattern facet in XML Schema, which, in turn, is based on the established conventions used in languages such as Perl. The meaning of the
flags
argument is also described in Chapter 14, on page 925.

Note that whereas the pattern facet in XML Schema uses a match that is implicitly anchored to the ends of the string, this function does not. A pattern specified in XML Schema must match the entire string to be successful; the regex specified in this function only needs to match some substring. For example,
#[0-9]+
will match a string if it contains as a substring a
#
character followed by one or more digits. If you want to test whether the entire string takes the form of a
#
character followed by one or more digits, use the regex

#[0-9]+$
.

If the regular expression or the flags argument does not conform to the specified syntax, a fatal error is reported at runtime (or at compile time, if it can be detected then).

Examples

Assume that
$e
is the following element:

A grand little lad was young Albert

All dressed in his best, quite a swell

With a stick with an horse's head handle

The finest that Woolworth's could sell.

Expression
Result
matches($e, “grand”)
true
matches($e, “

The finest”, “m”)
true
matches($e, “(

.*$)*”, “m”)
true
matches($e, “Albert.*Woolworth's”, “s”)
true
matches($e, “woolworth's”, “i”)
true
matches(“banana”, “

(.a)+$”)
true
matches(“23 May 2008”, “

[0-9]+\s[A-Z][a-z]+\s[0-9]+$”)
true
matches(“”, “a*”)
true

In XSLT, the fourth example might be written:


Note the use of two apostrophes to represent a single apostrophe within a string literal.

Usage

The
matches()
function provides a powerful alternative to the
contains()
,
starts-with()
, and
ends-with()
functions. It might be more expensive, but this is only likely to make a difference if searching a large amount of text, or when using an unusually complex regular expression.

Other books

Myles Away From Dublin by Flann O'Brien
Chasing Perfection Vol. 2 by Parker, M. S.
Seduced by the CEO by Lexie Davis
Things I Can't Forget by Miranda Kenneally