XQuery 和 XPath 正则表达式XQuery 和 XPath 正则表达式XQuery 和 XPath 正则表达式XQuery 和 XPath 正则表达式
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
XQuery 和 XPath 正则表达式
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式工具和实用程序 » XQuery 和 XPath 正则表达式

正则表达式工具
grep
语言和函数库
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
数据库
MySQL
Oracle
PostgreSQL
更多此网站内容
简介
正则表达式快速开始
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

XQuery 和 XPath 正则表达式

XQuery 1.0 和 XPath 2.0 函数和操作符的 W3C 标准定义了三个函数 fn:matches、fn:replace 和 fn:tokenize,它们将正则表达式作为其中一个参数。XQuery 和 XPath 标准为此目的引入了新的正则表达式风格。此风格与 XML Schema 风格相同,但增加了许多现代正则表达式风格中可用的功能,而 XML Schema 风格中没有。所有有效的 XML Schema 正则表达式也都是有效的 XQuery/XPath 正则表达式。反之则不一定成立。

由于 XML Schema 风格仅用于真/假有效性测试,因此这些功能已因性能原因而移除。XQuery 和 XPath 函数运行更复杂的正则表达式操作符,需要功能更丰富的正则表达式风格。话虽如此,XQuery 和 XPath 正则表达式风格仍受到现代标准的限制。

XQuery 和 XPath 支持 XML Schema 风格中功能之上的下列功能

  • ^ 和 $ 锚点,与字符串的开头或结尾,或行的开头或结尾相符(请参阅下列的相符模式)。这是支持的仅有两个锚点。
  • 惰性量词,使用熟悉的问号语法。
  • 反向引用和捕获组。XML Schema 标准支持群组,但群组一直是非截取的。XQuery/XPath 允许在正则表达式中使用反向引用。fn:replaces 使用 $1 表示法,在 替换文本 中支持反向引用。

XML Schema 完全不允许比对模式,而 XQuery 和 XPath 函数都接受一个选用的 flags 参数来设置比对模式。正则表达式内的 模式修改器 不受支持。这四种比对模式可用

  • i 使正则表达式比对不区分大小写。
  • s 激活「单行模式」。在此模式中,点号比对换行符。
  • m 激活「多行模式」。在此模式中,插入符号和美元符号在主旨字符串中的换行符号前后比对。
  • x 激活「自由间距模式」。在此模式中,正则表达式标记之间的空白会被忽略。不支持注解。

旗标指定为一个字符串,其中包含您想要激活的模式字母。例如 "ix" 激活不区分大小写和自由间距。如果您不想设置任何比对模式,您可以传递一个空字符串给 flags 参数,或完全省略该参数。

三个正则表达式函数

fn:matches(subject, pattern, flags) 将主旨字符串和正则表达式作为输入。如果正则表达式比对主旨字符串的任何部分,函数会传回 true。如果完全无法比对,它会传回 false。如果您只想要在正则表达式比对整个主旨字符串时,函数传回 true,您需要使用 插入符号。

fn:replace(subject, pattern, replacement, flags) 将主旨字符串、正则表达式和替换字符串作为输入。它传回一个新的字符串,其中主旨字符串中所有与正则表达式模式比对的部分都已替换为替换文本。您可以使用 $1 到 $99 将捕获组重新插入替换中。$0 插入整个正则表达式比对。替换中的字面美元符号和反斜线必须用反斜线转义。

fn:replace 无法替换长度为零的比对。例如,fn:replace("test", "^", "prefix") 会引发错误,而不是像大多数编程语言中的基于正则表达式的搜索和替换一样传回「prefixtext」。

fn:tokenize(subject, pattern, flags) 类似于许多编程语言中的「分割」函数。它传回一个字符串数组,其中包含主旨中所有正则表达式比对之间的所有子字符串。数组不会包含正则表达式比对本身。如果正则表达式比对主旨字符串中的第一个或最后一个字符,则结果数组中的第一个或最后一个字符串将为空字符串。

fn:tokenize 也无法处理长度为 0 的正则表达式比对。

XQuery 和 XPath 正規表示式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » XQuery 和 XPath 正規表示式

正規表示式工具
grep
語言和函式庫
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
資料庫
MySQL
Oracle
PostgreSQL
更多此網站內容
簡介
正規表示式快速開始
正規表示式教學課程
替換字串教學課程
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

XQuery 和 XPath 正規表示式

XQuery 1.0 和 XPath 2.0 函數和運算子的 W3C 標準定義了三個函數 fn:matches、fn:replace 和 fn:tokenize,它們將正規表示式作為其中一個參數。XQuery 和 XPath 標準為此目的引入了新的正規表示式風格。此風格與 XML Schema 風格相同,但增加了許多現代正規表示式風格中可用的功能,而 XML Schema 風格中沒有。所有有效的 XML Schema 正規表示式也都是有效的 XQuery/XPath 正規表示式。反之則不一定成立。

由於 XML Schema 風格僅用於真/假有效性測試,因此這些功能已因效能原因而移除。XQuery 和 XPath 函數執行更複雜的正規表示式運算子,需要功能更豐富的正規表示式風格。話雖如此,XQuery 和 XPath 正規表示式風格仍受到現代標準的限制。

XQuery 和 XPath 支援 XML Schema 風格中功能之上的下列功能

  • ^ 和 $ 錨點,與字串的開頭或結尾,或行的開頭或結尾相符(請參閱下列的相符模式)。這是支援的僅有兩個錨點。
  • 惰性量詞,使用熟悉的問號語法。
  • 反向參照和擷取群組。XML Schema 標準支援群組,但群組一直是非擷取的。XQuery/XPath 允許在正規表示式中使用反向參照。fn:replaces 使用 $1 表示法,在 替換文字 中支援反向參照。

XML Schema 完全不允許比對模式,而 XQuery 和 XPath 函式都接受一個選用的 flags 參數來設定比對模式。正規表示式內的 模式修改器 不受支援。這四種比對模式可用

  • i 使正規表示式比對不區分大小寫。
  • s 啟用「單行模式」。在此模式中,點號比對換行符。
  • m 啟用「多行模式」。在此模式中,插入符號和美元符號在主旨字串中的換行符號前後比對。
  • x 啟用「自由間距模式」。在此模式中,正規表示式標記之間的空白會被忽略。不支援註解。

旗標指定為一個字串,其中包含您想要啟用的模式字母。例如 "ix" 啟用不區分大小寫和自由間距。如果您不想設定任何比對模式,您可以傳遞一個空字串給 flags 參數,或完全省略該參數。

三個正規表示式函式

fn:matches(subject, pattern, flags) 將主旨字串和正規表示式作為輸入。如果正規表示式比對主旨字串的任何部分,函式會傳回 true。如果完全無法比對,它會傳回 false。如果您只想要在正規表示式比對整個主旨字串時,函式傳回 true,您需要使用 插入符號。

fn:replace(subject, pattern, replacement, flags) 將主旨字串、正規表示式和替換字串作為輸入。它傳回一個新的字串,其中主旨字串中所有與正規表示式模式比對的部分都已替換為替換文字。您可以使用 $1 到 $99 將擷取群組重新插入替換中。$0 插入整個正規表示式比對。替換中的字面美元符號和反斜線必須用反斜線跳脫。

fn:replace 無法替換長度為零的比對。例如,fn:replace("test", "^", "prefix") 會引發錯誤,而不是像大多數程式語言中的基於正規表示式的搜尋和替換一樣傳回「prefixtext」。

fn:tokenize(subject, pattern, flags) 類似於許多程式語言中的「分割」函式。它傳回一個字串陣列,其中包含主旨中所有正規表示式比對之間的所有子字串。陣列不會包含正規表示式比對本身。如果正規表示式比對主旨字串中的第一個或最後一個字元,則結果陣列中的第一個或最後一個字串將為空字串。

fn:tokenize 也無法處理長度為 0 的正規表示式比對。

XQuery and XPath Regular Expressions
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » XQuery and XPath Regular Expressions

Regex Tools
grep
Languages & Libraries
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery & XPath
XRegExp
Databases
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

XQuery and XPath Regular Expressions

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 is identical to the XML Schema flavor, with the addition of 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:

  • ^ and $ anchors that match at the start or end of the string, or the start or end of a line (see matching modes below). These are the only two anchors supported.
  • Lazy quantifiers, using the familiar question mark syntax.
  • Backreferences and capturing groups. The XML Schema standard supports grouping, but groups were always non-capturing. XQuery/XPath allows backreferences to be used in the regular expression. fn:replaces supports backreferences in the replacement text using the $1 notation.

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:

  • i makes the regex match case insensitive.
  • s enables “single-line mode”. In this mode, the dot matches newlines.
  • m enables “multi-line mode”. In this mode, the caret and dollar match before and after newlines in the subject string.
  • x enables “free-spacing mode”. In this mode, whitespace between regex tokens is ignored. Comments are not supported.

The flags are specified as a string with the letters of the modes you want to turn on. E.g. "ix" turns on case insensitivity and free-spacing. If you don’t want to set any matching modes, you can pass an empty string for the flags parameter, or omit the parameter entirely.

Three Regex Functions

fn:matches(subject, pattern, flags) takes a subject string and a regular expression as input. If the regular expression matches any part of the subject 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 subject string.

fn:replace(subject, pattern, replacement, flags) takes a subject string, a regular expression, and a replacement string as input. It returns a new string that is the subject 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. E.g. fn:replace("test", "^", "prefix") will raise an error rather than returning “prefixtext” like regex-based search-and-replace does in most programming languages.

fn:tokenize(subject, pattern, flags) is like the “split” function in many programming languages. It returns an array of strings that consists of all the substrings in subject 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 subject 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.

©2015-2025 艾丽卡 support@alaica.com