home contribute faq download

FunctX XSLT Functions

fn:matches

Whether a string matches a regular expression

Google
Webxsltfunctions.com

Description

The fn:matches function determines whether a string matches a regular expression. The regular expression syntax used is defined by XML Schema with a few modifications/additions in XQueryXPath/XSLT 2.0. The $pattern argument is a regular expression. Unlike many of the string-related functions, the fn:matches function does not use collations at all. Regular expression matching is solely based on Unicode code points. Unless the anchors "^" or "$" are used, the function returns true if any substring of $input matches the regular expression.

The $flags parameter allows for additional options in the interpretation of the regular expression. Flags and regular expressions are covered in detail in chapter 18 of the book XQuery.

For more examples of XQueryXPath/XSLT 2.0/XML Schema regular expressions, see this page.

This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book.

Arguments and Return Type

NameTypeDescription
$input xs:string? the string to test
$pattern xs:string the regular expression to test for
$flags xs:string flags that control multiline mode, case insensitivity, etc.
return value xs:boolean

Examples

<xsl:variable name="address" as="item()*">
'123123 Main Street
Traverse City, MI 49684'
</xsl:variable>
XPath ExampleResults
matches('query', 'q')
true
matches('query', 'ue')
true
matches('query', '^qu')
true
matches('query', 'qu$')
false
matches('query', '[ux]')
true
matches('query', 'q.*')
true
matches('query', '[a-z]{5}')
true
matches((), 'q' )
false
matches('query', '[qu')
Error FORX0002
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, 'street$', 'im')
true
matches($address, 'Street$', 'q')
Error FORX0001

See Also

fn:containsWhether one string contains another
functx:number-of-matchesThe number of regions that match a pattern

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XSLT Services

Recommended Reading:

XQuery