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

加载更多搜索结果...

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

正则表达式工具
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
本网站更多信息
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

POSIX 基本正则表达式

POSIX 或「uniX 的可携式操作系统接口」是一系列标准,定义 (UNIX) 操作系统应支持的部分功能。其中一项标准定义了两种正则表达式风格。grep 和 egrep 等涉及正则表达式的指令在兼容 POSIX 的 UNIX 系统上实作这些风格。多个数据库系统也使用 POSIX 正则表达式。

基本正则表达式或 BRE 风格标准化一种与传统 UNIX grep 指令所使用的风格类似的风格。这几乎是现今仍在使用的最古老的正则表达式风格。让这种风格与众不同的一点是,大多数的后设字符需要反斜线才能赋予后设字符其风格。包括 POSIX ERE 在内的大多数其他风格使用反斜线来抑制后设字符的意义。使用反斜线来转义从未是后设字符的字符是一种错误。

BRE 支持POSIX 方括号表达式,这类似于其他正则表达式风格中的字符类别,具有几个特殊功能。不支持简写。使用一般后设字符的其他功能包括点来比对任何字符(换行符号除外),插入符号和美元符号来比对字符串的开头和结尾,以及星号来重复令牌零次或多次。若要逐字比对这些字符中的任何一个,请使用反斜线转义它们。

其他 BRE 元字符需要反斜线才能赋予它们特殊意义。原因是 UNIX grep 的最旧版本不支持这些元字符。grep 的开发人员想要让它与现有的正则表达式兼容,而这些正则表达式可能会将这些字符用作字面字符。BRE a{1,2} 会将 a{1,2} 视为字面值,而 a\{1,2\} 会比对 a 或 aa。有些实作支持 \? 和 \+ 作为 \{0,1\} 和 \{1,\} 的替代语法,但 \? 和 \+ 并非 POSIX 标准的一部分。代币可以用 \( 和 \) 分组。反向引用是常见的 \1 到 \9。最多只允许 9 个群组。例如,\(ab\)\1 会比对 abab,而 (ab)\1 无效,因为没有对应于反向引用 \1 的捕获组。使用 \\1 来将 \1 视为字面值。

POSIX BRE 不支持任何其他功能。甚至 交替 也不支持。

POSIX 扩展正则表达式

扩展正则表达式或 ERE 风格标准化了类似 UNIX egrep 指令所使用的风格。「延伸」是相对于原始 UNIX grep,它只有方括号表达式、点、插入符号、美元符号和星号。ERE 支持这些内容,就像 BRE 一样。大多数现代正则表达式风格都是 ERE 风格的延伸。根据今日的标准,POSIX ERE 风格相当阳春。POSIX 标准定义于 1986 年,而正则表达式自此已发展得相当成熟。

egrep 的开发人员没有尝试维持与 grep 的兼容性,而是创建了一个独立的工具。因此,egrep 和 POSIX ERE 加入额外的元字符,而没有反斜线。你可以使用反斜线来抑制所有元字符的意义,就像在现代正则表达式风格中一样。将非元字符转义为错误。

量词 ?、+、{n}、{n,m} 和 {n,} 分别重复前一个代币 0 次或 1 次、1 次或多次、n 次、n 到 m 次,以及 n 次或更多次。交替由一般的垂直线 | 支持。未加装饰的括号会创建一个群组,例如 (abc){2} 符合 abcabc。POSIX 标准未定义反向引用。有些实作支持 \1 到 \9,但这些并非 ERE 标准的一部分。ERE 是旧 UNIX grep 的延伸,而非 POSIX BRE。

而延伸就只到此为止。

POSIX ERE 交替传回最长符合项

在关于交替的教学主题中,我解释了正则表达式引擎会在找到符合的替代项后立即停止。然而,POSIX 标准强制传回最长符合项。将 Set|SetValue 套用至 SetValue 时,符合 POSIX 的正则表达式引擎会完全符合 SetValue。即使引擎是正则表达式导向 NFA 引擎,POSIX 仍要求它通过尝试所有替代项,并传回最长符合项(此情况为 SetValue)来仿真DFA 文本导向符合。传统 NFA 引擎会符合 Set,就像本网站上讨论的所有其他正则表达式风格一样。

符合 POSIX 的引擎仍会找到最左符合项。如果您将 Set|SetValue 套用至 Set or SetValue 一次,它会符合 Set。字符串中的第一个位置是我们的正则表达式可以找到有效符合项的最左位置。在字符串中可以找到更长符合项的事实无关紧要。如果您再次套用正则表达式,并从字符串中的第一个空格继续,则会符合 SetValue。传统 NFA 引擎会在字符串开头符合 Set 作为第一个符合项,并在字符串中第 3 个字的开头符合 Set 作为第二个符合项。

POSIX 基本正規表示式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » POSIX 基本正規表示式

正規表示式工具
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
本網站更多資訊
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

POSIX 基本正規表示式

POSIX 或「uniX 的可攜式作業系統介面」是一系列標準,定義 (UNIX) 作業系統應支援的部分功能。其中一項標準定義了兩種正規表示式風格。grep 和 egrep 等涉及正規表示式的指令在相容 POSIX 的 UNIX 系統上實作這些風格。多個資料庫系統也使用 POSIX 正規表示式。

基本正規表示式或 BRE 風格標準化一種與傳統 UNIX grep 指令所使用的風格類似的風格。這幾乎是現今仍在使用的最古老的正規表示式風格。讓這種風格與眾不同的一點是,大多數的後設字元需要反斜線才能賦予後設字元其風格。包括 POSIX ERE 在內的大多數其他風格使用反斜線來抑制後設字元的意義。使用反斜線來跳脫從未是後設字元的字元是一種錯誤。

BRE 支援POSIX 方括號表示式,這類似於其他正規表示式風格中的字元類別,具有幾個特殊功能。不支援簡寫。使用一般後設字元的其他功能包括點來比對任何字元(換行符號除外),插入符號和美元符號來比對字串的開頭和結尾,以及星號來重複令牌零次或多次。若要逐字比對這些字元中的任何一個,請使用反斜線跳脫它們。

其他 BRE 元字元需要反斜線才能賦予它們特殊意義。原因是 UNIX grep 的最舊版本不支援這些元字元。grep 的開發人員想要讓它與現有的正規表示式相容,而這些正規表示式可能會將這些字元用作字面字元。BRE a{1,2} 會將 a{1,2} 視為字面值,而 a\{1,2\} 會比對 a 或 aa。有些實作支援 \? 和 \+ 作為 \{0,1\} 和 \{1,\} 的替代語法,但 \? 和 \+ 並非 POSIX 標準的一部分。代幣可以用 \( 和 \) 分組。反向參照是常見的 \1 到 \9。最多只允許 9 個群組。例如,\(ab\)\1 會比對 abab,而 (ab)\1 無效,因為沒有對應於反向參照 \1 的擷取群組。使用 \\1 來將 \1 視為字面值。

POSIX BRE 不支援任何其他功能。甚至 交替 也不支援。

POSIX 延伸正規表示式

延伸正規表示式或 ERE 風格標準化了類似 UNIX egrep 指令所使用的風格。「延伸」是相對於原始 UNIX grep,它只有方括號表示式、點、插入符號、美元符號和星號。ERE 支援這些內容,就像 BRE 一樣。大多數現代正規表示式風格都是 ERE 風格的延伸。根據今日的標準,POSIX ERE 風格相當陽春。POSIX 標準定義於 1986 年,而正規表示式自此已發展得相當成熟。

egrep 的開發人員沒有嘗試維持與 grep 的相容性,而是建立了一個獨立的工具。因此,egrep 和 POSIX ERE 加入額外的元字元,而沒有反斜線。你可以使用反斜線來抑制所有元字元的意義,就像在現代正規表示式風格中一樣。將非元字元轉義為錯誤。

量詞 ?、+、{n}、{n,m} 和 {n,} 分別重複前一個代幣 0 次或 1 次、1 次或多次、n 次、n 到 m 次,以及 n 次或更多次。交替由一般的垂直線 | 支援。未加裝飾的括號會建立一個群組,例如 (abc){2} 符合 abcabc。POSIX 標準未定義反向參照。有些實作支援 \1 到 \9,但這些並非 ERE 標準的一部分。ERE 是舊 UNIX grep 的延伸,而非 POSIX BRE。

而延伸就只到此為止。

POSIX ERE 交替傳回最長符合項

在關於交替的教學主題中,我解釋了正規表示式引擎會在找到符合的替代項後立即停止。然而,POSIX 標準強制傳回最長符合項。將 Set|SetValue 套用至 SetValue 時,符合 POSIX 的正規表示式引擎會完全符合 SetValue。即使引擎是正規表示式導向 NFA 引擎,POSIX 仍要求它透過嘗試所有替代項,並傳回最長符合項(此情況為 SetValue)來模擬DFA 文字導向符合。傳統 NFA 引擎會符合 Set,就像本網站上討論的所有其他正規表示式風格一樣。

符合 POSIX 的引擎仍會找到最左符合項。如果您將 Set|SetValue 套用至 Set or SetValue 一次,它會符合 Set。字串中的第一個位置是我們的正規表示式可以找到有效符合項的最左位置。在字串中可以找到更長符合項的事實無關緊要。如果您再次套用正規表示式,並從字串中的第一個空格繼續,則會符合 SetValue。傳統 NFA 引擎會在字串開頭符合 Set 作為第一個符合項,並在字串中第 3 個字的開頭符合 Set 作為第二個符合項。

POSIX Basic Regular Expressions
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » POSIX Basic 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

POSIX Basic Regular Expressions

POSIX or “Portable Operating System Interface for uniX” is a collection of standards that define some of the functionality that a (UNIX) operating system should support. One of these standards defines two flavors of regular expressions. Commands involving regular expressions, such as grep and egrep, implement these flavors on POSIX-compliant UNIX systems. Several database systems also use POSIX regular expressions.

The Basic Regular Expressions or BRE flavor standardizes a flavor similar to the one used by the traditional UNIX grep command. This is pretty much the oldest regular expression flavor still in use today. One thing that sets this flavor apart is that most metacharacters require a backslash to give the metacharacter its flavor. Most other flavors, including POSIX ERE, use a backslash to suppress the meaning of metacharacters. Using a backslash to escape a character that is never a metacharacter is an error.

A BRE supports POSIX bracket expressions, which are similar to character classes in other regex flavors, with a few special features. Shorthands are not supported. Other features using the usual metacharacters are the dot to match any character except a line break, the caret and dollar to match the start and end of the string, and the star to repeat the token zero or more times. To match any of these characters literally, escape them with a backslash.

The other BRE metacharacters require a backslash to give them their special meaning. The reason is that the oldest versions of UNIX grep did not support these. The developers of grep wanted to keep it compatible with existing regular expressions, which may use these characters as literal characters. The BRE a{1,2} matches a{1,2} literally, while a\{1,2\} matches a or aa. Some implementations support \? and \+ as an alternative syntax to \{0,1\} and \{1,\}, but \? and \+ are not part of the POSIX standard. Tokens can be grouped with \( and \). Backreferences are the usual \1 through \9. Only up to 9 groups are permitted. E.g. \(ab\)\1 matches abab, while (ab)\1 is invalid since there’s no capturing group corresponding to the backreference \1. Use \\1 to match \1 literally.

POSIX BRE does not support any other features. Even alternation is not supported.

POSIX Extended Regular Expressions

The Extended Regular Expressions or ERE flavor standardizes a flavor similar to the one used by the UNIX egrep command. “Extended” is relative to the original UNIX grep, which only had bracket expressions, dot, caret, dollar and star. An ERE support these just like a BRE. Most modern regex flavors are extensions of the ERE flavor. By today’s standard, the POSIX ERE flavor is rather bare bones. The POSIX standard was defined in 1986, and regular expressions have come a long way since then.

The developers of egrep did not try to maintain compatibility with grep, creating a separate tool instead. Thus egrep, and POSIX ERE, add additional metacharacters without backslashes. You can use backslashes to suppress the meaning of all metacharacters, just like in modern regex flavors. Escaping a character that is not a metacharacter is an error.

The quantifiers ?, +, {n}, {n,m} and {n,} repeat the preceding token zero or once, once or more, n times, between n and m times, and n or more times, respectively. Alternation is supported through the usual vertical bar |. Unadorned parentheses create a group, e.g. (abc){2} matches abcabc. The POSIX standard does not define backreferences. Some implementations do support \1 through \9, but these are not part of the standard for ERE. ERE is an extension of the old UNIX grep, not of POSIX BRE.

And that’s exactly how far the extension goes.

POSIX ERE Alternation Returns The Longest Match

In the tutorial topic about alternation, I explained that the regex engine will stop as soon as it finds a matching alternative. The POSIX standard, however, mandates that the longest match be returned. When applying Set|SetValue to SetValue, a POSIX-compliant regex engine will match SetValue entirely. Even if the engine is a regex-directed NFA engine, POSIX requires that it simulates DFA text-directed matching by trying all alternatives, and returning the longest match, in this case SetValue. A traditional NFA engine would match Set, as do all other regex flavors discussed on this website.

A POSIX-compliant engine will still find the leftmost match. If you apply Set|SetValue to Set or SetValue once, it will match Set. The first position in the string is the leftmost position where our regex can find a valid match. The fact that a longer match can be found further in the string is irrelevant. If you apply the regex a second time, continuing at the first space in the string, then SetValue will be matched. A traditional NFA engine would match Set at the start of the string as the first match, and Set at the start of the 3rd word in the string as the second match.

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