| Regex Tools |
| grep |
| PowerGREP |
| RegexBuddy |
| RegexMagic |
| General Applications |
| EditPad Lite |
| EditPad Pro |
| Google Docs |
| Google Sheets |
| LibreOffice |
| Notepad++ |
| Databases |
| Google BigQuery |
| MySQL |
| Oracle |
| PostgreSQL |
The W3C standard for XQuery 1.0 and XPath 2.0 Functions and Operators defines three functions fn:matches, fn:replace, and fn:tokenize that take a regular expression as one of their parameters. The XQuery and XPath standard introduces a new regular expression flavor for this purpose. This flavor extends the XML Schema flavor with several features that are available in many modern regex flavors, but not in the XML Schema flavor. All valid XML Schema regexes are also valid XQuery/XPath regexes. The opposite is not always true.
Because the XML Schema flavor is only used for true/false validity tests, these features were eliminated for performance reasons. The XQuery and XPath functions perform more complex regular expression operators, which require a more feature-rich regular expression flavor. That said, the XQuery and XPath regex flavor is still limited by modern standards.
XQuery and XPath support the following features on top of the features in the XML Schema flavor:
While XML Schema allows no matching modes at all, the XQuery and XPath functions all accept an optional flags parameter to set matching modes. Mode modifiers within the regular expression are not supported. These four matching modes are available:
The flags are specified as a string with the letters of the modes you want to turn on. For example, "ix" turns on case insensitivity and free-spacing. If you don’t want to set any matching modes then you can pass an empty string for the flags parameter, or omit the parameter entirely.
The XML and XPath entries in the regular expressions reference on this website are based on the actual XSD and XPath implementations provided by Saxon.
fn:matches(input, pattern, flags) takes a input string and a regular expression as input. If the regular expression matches any part of the input string, the function returns true. If it cannot match at all, it returns false. You’ll need to use anchors if you only want the function to return true when the regex matches the entire input string.
fn:replace(input, pattern, replacement, flags) takes a input string, a regular expression, and a replacement string as input. It returns a new string that is the input string with all matches of the regex pattern replaced with the replacement text. You can use $1 to $99 to re-insert capturing groups into the replacement. $0 inserts the whole regex match. Literal dollar signs and backslashes in the replacement must be escaped with a backslash.
fn:replace cannot replace zero-length matches. fn:replace("test", "^", "prefix"), for example, raises an error rather than returning “prefixtest” like regex-based search-and-replace does in most programming languages. Saxon’s implementation checks whether a regex has the potential to find zero-length matches, regardless of the input string, before the search-and-replace even begins. This makes fn:replace("123", "^\d*$", "number", "m") an error even though ^\d*$ only finds one non-zero-length match 123 in this example. The potential of this regex to find a zero-length match in a string with two consecutive newline characters is enough for Saxon’s fn:replace to reject the regex.
fn:replace has no problems with backreferences to capturing groups that find zero-length matches. Such backreferences and even backreferences to non-participating and non-existing capturing groups are silently removed from the replacement. If the regex does not find any matches in the input string at all then it simply returns the original string.
fn:tokenize(input, pattern, flags) is like the “split” function in many programming languages. It returns an array of strings that consists of all the substrings in input between all the regex matches. The array will not contain the regex matches themselves. If the regex matches the first or last character in the input string, then the first or last string in the resulting array will be empty strings.
fn:tokenize also cannot handle zero-length regular expression matches.
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| grep | PowerGREP | RegexBuddy | RegexMagic |
| EditPad Lite | EditPad Pro | Google Docs | Google Sheets | LibreOffice | Notepad++ |
| Boost | C# | Delphi | F# | GNU (Linux) | Groovy | ICU (Unicode) | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | Python.NET and IronPython | R | RE2 | Ruby | std::regex | Tcl | TypeScript | VBScript | Visual Basic 6 | Visual Basic (.NET) | wxWidgets | XML Schema | XQuery & XPath | Xojo | XRegExp |
| Google BigQuery | MySQL | Oracle | PostgreSQL |
Page URL: https://www.regular-expressions.info/xpath.html
Page last updated: 6 August 2025
Site last updated: 29 October 2025
Copyright © 2003-2025 Jan Goyvaerts. All rights reserved.