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

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
POSIX 中括号表达式
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式教程 » POSIX 中括号表达式

正则表达式教程
简介
目录
特殊字符
不可打印字符
正则表达式引擎内部
字符类别
字符类别减法
字符类别交集
简写字符类别
点
锚定
字词边界
交替
选用项目
重复
群组和截取
反向引用
反向引用,第 2 部分
命名组
相对反向引用
分支重设群组
自由间距和注解
Unicode
模式修改器
原子组
独占量词
前瞻和后顾
环顾,第 2 部分
将文本排除在比对之外
条件
平衡组
递归
子常式
无限递归
递归和量词
递归和截取
递归和反向引用
递归和回溯
POSIX 中括号表达式
零长度比对
持续比对
本网站的更多内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

POSIX 中括号表达式

POSIX 中括号表达式是一种特殊的字符类别。POSIX 中括号表达式会比对一组字符中的其中一个字符,就像一般的字符类别一样。它们使用相同的方括号语法。连字号会创建一个范围,而开头的插入符号会否定中括号表达式。

一个主要的语法差异是,在 POSIX 中括号表达式中,反斜线并非一个元字符。因此,在 POSIX 中,正则表达式[\d]会比对\或d。若要比对],请将其放在开头[或否定^之后的第一个字符。若要比对-,请将其放在闭合]之前。若要比对^,请将其放在最后一个字面-或闭合]之前。组合起来,[\d^-]会比对]、\、d、^或-。

中括号表达式的主要目的是适应用户的语言环境或应用程序的语言环境。语言环境是一组规则和设置,用来描述语言和文化惯例,例如排序顺序、日期格式等。POSIX 标准定义了这些语言环境。

一般来说,只有 符合 POSIX 的正则表达式引擎 才对 POSIX 中括号表达式有适当且完整的支持。有些非 POSIX 正则表达式引擎支持 POSIX 字符类别,但通常不支持整理串行和等效字符。支持 Unicode 的正则表达式引擎使用 Unicode 属性和脚本来提供类似于 POSIX 中括号表达式的功能。在 Unicode 正则表达式引擎中,简写字符类别(例如 \w)通常会比对所有相关的 Unicode 字符,减轻使用区域设置的需求。

字符类别

不要将 POSIX 术语「字符类别」与通常称为 正则表达式字符类别 的东西混淆。[x-z0-9] 是本教程中称为「字符类别」的范例,而 POSIX 称为「中括号表达式」。[:digit:] 是 POSIX 字符类别,用在中括号表达式中,例如 [x-z[:digit:]]。POSIX 字符类别名称必须全部以小写写入。

当用于 ASCII 字符串时,这两个正则表达式会找到完全相同的比对结果:一个字符,可能是 x、y、z 或数字。当用于含有非 ASCII 字符的字符串时,[:digit:] 类别可能会包含其他脚本中的数字,视区域设置而定。

POSIX 标准定义了 12 个字符类别。下表列出全部 12 个类别,以及某些正则表达式风格也支持的 [:ascii:] 和 [:word:] 类别。此表也显示您可以用在 ASCII 和 Unicode 正则表达式中的等效字符类别(如果 POSIX 类别不可用)。ASCII 等效字符与 POSIX 标准中定义的内容完全对应。Unicode 等效字符与大多数 Unicode 正则表达式引擎比对的内容对应。POSIX 标准未定义 Unicode 区域设置。有些类别也有 Perl 风格的 简写 等效字符。

Java 不支持 POSIX 中括号表达式,但使用 \p 算子支持 POSIX 字符类别。尽管 \p 语法是从 Unicode 属性 的语法借来的,但 Java 中的 POSIX 类别只比对 ASCII 字符,如下所示。类别名称区分大小写。与只能用于中括号表达式中的 POSIX 语法不同,Java 的 \p 可以用于中括号表达式内外。

在 Java 8 及之前版本中,无论您是否在 \p 语法中使用 Is 前缀都没有关系。因此在 Java 8 中,\p{Alnum} 和 \p{IsAlnum} 是相同的。在 Java 9 及之后版本中则有所不同。如果不使用 Is 前缀,行为与之前版本的 Java 完全相同。现在,使用 Is 前缀的语法也会比对 Unicode 字符。对于 \p{IsPunct},这也表示它不再比对符号 Unicode 类别中的 ASCII 字符。

POSIX说明ASCIIUnicode简写Java
[:alnum:] 字母数字字符 [a-zA-Z0-9] [\p{L}\p{Nl}
 
\p{Nd}]
\p{Alnum}
[:alpha:] 字母字符 [a-zA-Z] \p{L}\p{Nl} \p{Alpha}
[:ascii:] ASCII 字符 [\x00-\x7F] \p{InBasicLatin} \p{ASCII}
[:blank:] 空白和 tab [ \t] [\p{Zs}\t] \h \p{Blank}
[:cntrl:] 控制字符 [\x00-\x1F\x7F] \p{Cc} \p{Cntrl}
[:digit:] 数字 [0-9] \p{Nd} \d \p{Digit}
[:graph:] 可见字符(任何字符,除了空白和控制字符) [\x21-\x7E] [^\p{Z}\p{C}] \p{Graph}
[:lower:] 小写字母 [a-z] \p{Ll} \l \p{Lower}
[:print:] 可见字符和空白(任何字符,除了控制字符) [\x20-\x7E] \P{C} \p{Print}
[:punct:] 标点符号(和符号)。 [!"\#$%&'()*+,
\-./:;<=>?@\[
\\\]^_`{|}~]
\p{P} \p{Punct}
[:space:] 所有空白字符,包括换行 [ \t\r\n\v\f] [\p{Z}\t\r\n\v\f] \s \p{Space}
[:upper:] 大写字母 [A-Z] \p{Lu} \u \p{Upper}
[:word:] 字符字符(字母、数字和底线) [A-Za-z0-9_] [\p{L}\p{Nl}
 
\p{Nd}\p{Pc}]
\w \p{IsWord}
[:xdigit:] 十六进位数字 [A-Fa-f0-9] [A-Fa-f0-9] \p{XDigit}
POSIX说明ASCIIUnicode简写Java

对照串行

POSIX 地区设置可以有排序顺序,用来描述特定字符或字符群组应如何排序。例如在捷克语中,ch 如同 chemie(捷克语中的「化学」)是一个 二合字母。这表示它应视为一个字符。在捷克语字母表中,它的排序在 h 和 i 之间。当捷克语地区设置 (cs-CZ) 激活时,您可以在方括号表达式中使用排序顺序元素 [.ch.] 来比对 ch。正则表达式 [[.ch.]]emie 比对 chemie。请注意双重方括号。一对用于方括号表达式,另一对用于排序顺序。

除了 POSIX 兼容系统中 POSIX 兼容引擎之外,本教程中讨论的正则表达式风格都不支持排序顺序。

请注意,当地区设置设为捷克语时,完全 POSIX 兼容的正则表达式引擎会将 ch 视为单一字符。这表示 [^x]emie 也会比对 chemie。 [^x] 比对的字符不是 x,其中包括捷克语 POSIX 地区设置中的 ch。

在任何其他正则表达式引擎中,或是在不将 ch 视为二合字母的地区设置中使用 POSIX 引擎时,[^x]emie 会比对拼错的字词 cemie,但不会比对 chemie,因为 [^x] 无法比对两个字符 ch。

最后,请注意,并非所有宣称实作 POSIX 正则表达式的正则表达式引擎都真正完全支持排序顺序。有时,这些引擎会使用 POSIX 定义的正则表达式语法,但没有完整的区域设置支持。您可能想尝试上述比对,看看您使用的引擎是否支持。例如,Tcl 的 regexp 指令 支持排序顺序的语法。但 Tcl 只支持 Unicode 地区设置,而 Unicode 地区设置没有定义任何排序顺序。结果是,在 Tcl 中,指定单一字符的排序顺序只会比对该字符。所有其他排序顺序都会导致错误。

字符等效

POSIX 地区设置可以定义字符等效,表示某些字符在排序时应视为相同。例如在法语中,排序字词时会忽略重音符号。 élève 在 être 之前,而 être 在 événement 之前。 é 和 ê 都和 e 相同,但 l 在 t 之前,而 t 在 v 之前。当地区设置设为法语时,POSIX 兼容的正则表达式引擎会比对 e、é、è 和 ê,当您在方括号表达式 [[=e=]] 中使用排序顺序 [=e=] 时。

如果一个字符没有任何等价字符,字符等价符号只会还原成字符本身。例如,[[=x=][=z=]],在法语环境中,等同于 [xz]。

与校对串行类似,POSIX 字符等价在本文讨论的任何正则表达式引擎中都不可用,除非它们遵循 POSIX 标准。而那些遵循标准的引擎可能没有必要的 POSIX 环境支持。Tcl 的 regexp 指令也支持字符等价的语法。但是 Unicode 环境(Tcl 仅支持此环境)没有定义任何字符等价。这实际上表示,在 Tcl 中,[[=e=]] 和 [e] 完全相同,而且只会比对 e,无论您尝试使用哪个字符取代「e」。

POSIX 中括號表達式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » POSIX 中括號表達式

正規表示式教學
簡介
目錄
特殊字元
不可列印字元
正規表示式引擎內部
字元類別
字元類別減法
字元類別交集
簡寫字元類別
點
錨定
字詞邊界
交替
選用項目
重複
群組和擷取
反向參照
反向參照,第 2 部分
命名群組
相對反向參照
分支重設群組
自由間距和註解
Unicode
模式修改器
原子群組
獨佔量詞
前瞻和後顧
環顧,第 2 部分
將文字排除在比對之外
條件
平衡群組
遞迴
子常式
無限遞迴
遞迴和量詞
遞迴和擷取
遞迴和反向參照
遞迴和回溯
POSIX 中括號表達式
零長度比對
持續比對
本網站的更多內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

POSIX 中括號表達式

POSIX 中括號表達式是一種特殊的字元類別。POSIX 中括號表達式會比對一組字元中的其中一個字元,就像一般的字元類別一樣。它們使用相同的方括號語法。連字號會建立一個範圍,而開頭的插入符號會否定中括號表達式。

一個主要的語法差異是,在 POSIX 中括號表達式中,反斜線並非一個元字元。因此,在 POSIX 中,正規表示式[\d]會比對\或d。若要比對],請將其放在開頭[或否定^之後的第一個字元。若要比對-,請將其放在閉合]之前。若要比對^,請將其放在最後一個字面-或閉合]之前。組合起來,[\d^-]會比對]、\、d、^或-。

中括號表達式的主要目的是適應使用者的語言環境或應用程式的語言環境。語言環境是一組規則和設定,用來描述語言和文化慣例,例如排序順序、日期格式等。POSIX 標準定義了這些語言環境。

一般來說,只有 符合 POSIX 的正規表示式引擎 才對 POSIX 中括號表示式有適當且完整的支援。有些非 POSIX 正規表示式引擎支援 POSIX 字元類別,但通常不支援整理序列和等效字元。支援 Unicode 的正規表示式引擎使用 Unicode 屬性和腳本來提供類似於 POSIX 中括號表示式的功能。在 Unicode 正規表示式引擎中,簡寫字元類別(例如 \w)通常會比對所有相關的 Unicode 字元,減輕使用區域設定的需求。

字元類別

不要將 POSIX 術語「字元類別」與通常稱為 正規表示式字元類別 的東西混淆。[x-z0-9] 是本教學課程中稱為「字元類別」的範例,而 POSIX 稱為「中括號表示式」。[:digit:] 是 POSIX 字元類別,用在中括號表示式中,例如 [x-z[:digit:]]。POSIX 字元類別名稱必須全部以小寫寫入。

當用於 ASCII 字串時,這兩個正規表示式會找到完全相同的比對結果:一個字元,可能是 x、y、z 或數字。當用於含有非 ASCII 字元的字串時,[:digit:] 類別可能會包含其他腳本中的數字,視區域設定而定。

POSIX 標準定義了 12 個字元類別。下表列出全部 12 個類別,以及某些正規表示式風格也支援的 [:ascii:] 和 [:word:] 類別。此表也顯示您可以用在 ASCII 和 Unicode 正規表示式中的等效字元類別(如果 POSIX 類別不可用)。ASCII 等效字元與 POSIX 標準中定義的內容完全對應。Unicode 等效字元與大多數 Unicode 正規表示式引擎比對的內容對應。POSIX 標準未定義 Unicode 區域設定。有些類別也有 Perl 風格的 簡寫 等效字元。

Java 不支援 POSIX 中括號表示式,但使用 \p 算子支援 POSIX 字元類別。儘管 \p 語法是從 Unicode 屬性 的語法借來的,但 Java 中的 POSIX 類別只比對 ASCII 字元,如下所示。類別名稱區分大小寫。與只能用於中括號表示式中的 POSIX 語法不同,Java 的 \p 可以用於中括號表示式內外。

在 Java 8 及之前版本中,無論您是否在 \p 語法中使用 Is 字首都沒有關係。因此在 Java 8 中,\p{Alnum} 和 \p{IsAlnum} 是相同的。在 Java 9 及之後版本中則有所不同。如果不使用 Is 字首,行為與之前版本的 Java 完全相同。現在,使用 Is 字首的語法也會比對 Unicode 字元。對於 \p{IsPunct},這也表示它不再比對符號 Unicode 類別中的 ASCII 字元。

POSIX說明ASCIIUnicode簡寫Java
[:alnum:] 字母數字字元 [a-zA-Z0-9] [\p{L}\p{Nl}
 
\p{Nd}]
\p{Alnum}
[:alpha:] 字母字元 [a-zA-Z] \p{L}\p{Nl} \p{Alpha}
[:ascii:] ASCII 字元 [\x00-\x7F] \p{InBasicLatin} \p{ASCII}
[:blank:] 空白和 tab [ \t] [\p{Zs}\t] \h \p{Blank}
[:cntrl:] 控制字元 [\x00-\x1F\x7F] \p{Cc} \p{Cntrl}
[:digit:] 數字 [0-9] \p{Nd} \d \p{Digit}
[:graph:] 可見字元(任何字元,除了空白和控制字元) [\x21-\x7E] [^\p{Z}\p{C}] \p{Graph}
[:lower:] 小寫字母 [a-z] \p{Ll} \l \p{Lower}
[:print:] 可見字元和空白(任何字元,除了控制字元) [\x20-\x7E] \P{C} \p{Print}
[:punct:] 標點符號(和符號)。 [!"\#$%&'()*+,
\-./:;<=>?@\[
\\\]^_`{|}~]
\p{P} \p{Punct}
[:space:] 所有空白字元,包括換行 [ \t\r\n\v\f] [\p{Z}\t\r\n\v\f] \s \p{Space}
[:upper:] 大寫字母 [A-Z] \p{Lu} \u \p{Upper}
[:word:] 字元字元(字母、數字和底線) [A-Za-z0-9_] [\p{L}\p{Nl}
 
\p{Nd}\p{Pc}]
\w \p{IsWord}
[:xdigit:] 十六進位數字 [A-Fa-f0-9] [A-Fa-f0-9] \p{XDigit}
POSIX說明ASCIIUnicode簡寫Java

對照序列

POSIX 地區設定可以有排序順序,用來描述特定字元或字元群組應如何排序。例如在捷克語中,ch 如同 chemie(捷克語中的「化學」)是一個 二合字母。這表示它應視為一個字元。在捷克語字母表中,它的排序在 h 和 i 之間。當捷克語地區設定 (cs-CZ) 啟用時,您可以在方括號表達式中使用排序順序元素 [.ch.] 來比對 ch。正規表示法 [[.ch.]]emie 比對 chemie。請注意雙重方括號。一對用於方括號表達式,另一對用於排序順序。

除了 POSIX 相容系統中 POSIX 相容引擎之外,本教學課程中討論的正規表示法風格都不支援排序順序。

請注意,當地區設定設為捷克語時,完全 POSIX 相容的正規表示法引擎會將 ch 視為單一字元。這表示 [^x]emie 也會比對 chemie。 [^x] 比對的字元不是 x,其中包括捷克語 POSIX 地區設定中的 ch。

在任何其他正規表示法引擎中,或是在不將 ch 視為二合字母的地區設定中使用 POSIX 引擎時,[^x]emie 會比對拼錯的字詞 cemie,但不會比對 chemie,因為 [^x] 無法比對兩個字元 ch。

最後,請注意,並非所有宣稱實作 POSIX 正規表示法的正規表示法引擎都真正完全支援排序順序。有時,這些引擎會使用 POSIX 定義的正規表示法語法,但沒有完整的區域設定支援。您可能想嘗試上述比對,看看您使用的引擎是否支援。例如,Tcl 的 regexp 指令 支援排序順序的語法。但 Tcl 只支援 Unicode 地區設定,而 Unicode 地區設定沒有定義任何排序順序。結果是,在 Tcl 中,指定單一字元的排序順序只會比對該字元。所有其他排序順序都會導致錯誤。

字元等效

POSIX 地區設定可以定義字元等效,表示某些字元在排序時應視為相同。例如在法語中,排序字詞時會忽略重音符號。 élève 在 être 之前,而 être 在 événement 之前。 é 和 ê 都和 e 相同,但 l 在 t 之前,而 t 在 v 之前。當地區設定設為法語時,POSIX 相容的正規表示法引擎會比對 e、é、è 和 ê,當您在方括號表達式 [[=e=]] 中使用排序順序 [=e=] 時。

如果一個字元沒有任何等價字元,字元等價符號只會還原成字元本身。例如,[[=x=][=z=]],在法語環境中,等同於 [xz]。

與校對序列類似,POSIX 字元等價在本文討論的任何正規表示式引擎中都不可用,除非它們遵循 POSIX 標準。而那些遵循標準的引擎可能沒有必要的 POSIX 環境支援。Tcl 的 regexp 指令也支援字元等價的語法。但是 Unicode 環境(Tcl 僅支援此環境)沒有定義任何字元等價。這實際上表示,在 Tcl 中,[[=e=]] 和 [e] 完全相同,而且只會比對 e,無論您嘗試使用哪個字元取代「e」。

POSIX Bracket Expressions
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » POSIX Bracket Expressions

Regex Tutorial
Introduction
Table of Contents
Special Characters
Non-Printable Characters
Regex Engine Internals
Character Classes
Character Class Subtraction
Character Class Intersection
Shorthand Character Classes
Dot
Anchors
Word Boundaries
Alternation
Optional Items
Repetition
Grouping & Capturing
Backreferences
Backreferences, part 2
Named Groups
Relative Backreferences
Branch Reset Groups
Free-Spacing & Comments
Unicode
Mode Modifiers
Atomic Grouping
Possessive Quantifiers
Lookahead & Lookbehind
Lookaround, part 2
Keep Text out of The Match
Conditionals
Balancing Groups
Recursion
Subroutines
Infinite Recursion
Recursion & Quantifiers
Recursion & Capturing
Recursion & Backreferences
Recursion & Backtracking
POSIX Bracket Expressions
Zero-Length Matches
Continuing Matches
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 Bracket Expressions

POSIX bracket expressions are a special kind of character classes. POSIX bracket expressions match one character out of a set of characters, just like regular character classes. They use the same syntax with square brackets. A hyphen creates a range, and a caret at the start negates the bracket expression.

One key syntactic difference is that the backslash is NOT a metacharacter in a POSIX bracket expression. So in POSIX, the regular expression [\d] matches a \ or a d. To match a ], put it as the first character after the opening [ or the negating ^. To match a -, put it right before the closing ]. To match a ^, put it before the final literal - or the closing ]. Put together, []\d^-] matches ], \, d, ^ or -.

The main purpose of bracket expressions is that they adapt to the user’s or application’s locale. A locale is a collection of rules and settings that describe language and cultural conventions, like sort order, date format, etc. The POSIX standard defines these locales.

Generally, only POSIX-compliant regular expression engines have proper and full support for POSIX bracket expressions. Some non-POSIX regex engines support POSIX character classes, but usually don’t support collating sequences and character equivalents. Regular expression engines that support Unicode use Unicode properties and scripts to provide functionality similar to POSIX bracket expressions. In Unicode regex engines, shorthand character classes like \w normally match all relevant Unicode characters, alleviating the need to use locales.

Character Classes

Don’t confuse the POSIX term “character class” with what is normally called a regular expression character class. [x-z0-9] is an example of what this tutorial calls a “character class” and what POSIX calls a “bracket expression”. [:digit:] is a POSIX character class, used inside a bracket expression like [x-z[:digit:]]. The POSIX character class names must be written all lowercase.

When used on ASCII strings, these two regular expressions find exactly the same matches: a single character that is either x, y, z, or a digit. When used on strings with non-ASCII characters, the [:digit:] class may include digits in other scripts, depending on the locale.

The POSIX standard defines 12 character classes. The table below lists all 12, plus the [:ascii:] and [:word:] classes that some regex flavors also support. The table also shows equivalent character classes that you can use in ASCII and Unicode regular expressions if the POSIX classes are unavailable. The ASCII equivalents correspond exactly what is defined in the POSIX standard. The Unicode equivalents correspond to what most Unicode regex engines match. The POSIX standard does not define a Unicode locale. Some classes also have Perl-style shorthand equivalents.

Java does not support POSIX bracket expressions, but does support POSIX character classes using the \p operator. Though the \p syntax is borrowed from the syntax for Unicode properties, the POSIX classes in Java only match ASCII characters as indicated below. The class names are case sensitive. Unlike the POSIX syntax which can only be used inside a bracket expression, Java’s \p can be used inside and outside bracket expressions.

In Java 8 and prior, it does not matter whether you use the Is prefix with the \p syntax or not. So in Java 8, \p{Alnum} and \p{IsAlnum} are identical. In Java 9 and later there is a difference. Without the Is prefix, the behavior is exactly the same as in previous versions of Java. The syntax with the Is prefix now matches Unicode characters too. For \p{IsPunct} this also means that it no longer matches the ASCII characters that are in the Symbol Unicode category.

POSIXDescriptionASCIIUnicodeShorthandJava
[:alnum:] Alphanumeric characters [a-zA-Z0-9] [\p{L}\p{Nl}
 
\p{Nd}]
\p{Alnum}
[:alpha:] Alphabetic characters [a-zA-Z] \p{L}\p{Nl} \p{Alpha}
[:ascii:] ASCII characters [\x00-\x7F] \p{InBasicLatin} \p{ASCII}
[:blank:] Space and tab [ \t] [\p{Zs}\t] \h \p{Blank}
[:cntrl:] Control characters [\x00-\x1F\x7F] \p{Cc} \p{Cntrl}
[:digit:] Digits [0-9] \p{Nd} \d \p{Digit}
[:graph:] Visible characters (anything except spaces and control characters) [\x21-\x7E] [^\p{Z}\p{C}] \p{Graph}
[:lower:] Lowercase letters [a-z] \p{Ll} \l \p{Lower}
[:print:] Visible characters and spaces (anything except control characters) [\x20-\x7E] \P{C} \p{Print}
[:punct:] Punctuation (and symbols). [!"\#$%&'()*+,
\-./:;<=>?@\[
\\\]^_`{|}~]
\p{P} \p{Punct}
[:space:] All whitespace characters, including line breaks [ \t\r\n\v\f] [\p{Z}\t\r\n\v\f] \s \p{Space}
[:upper:] Uppercase letters [A-Z] \p{Lu} \u \p{Upper}
[:word:] Word characters (letters, numbers and underscores) [A-Za-z0-9_] [\p{L}\p{Nl}
 
\p{Nd}\p{Pc}]
\w \p{IsWord}
[:xdigit:] Hexadecimal digits [A-Fa-f0-9] [A-Fa-f0-9] \p{XDigit}
POSIXDescriptionASCIIUnicodeShorthandJava

Collating Sequences

A POSIX locale can have collating sequences to describe how certain characters or groups of characters should be ordered. In Czech, for example, ch as in chemie (“chemistry” in Czech) is a digraph. This means it should be treated as if it were one character. It is ordered between h and i in the Czech alphabet. You can use the collating sequence element [.ch.] inside a bracket expression to match ch when the Czech locale (cs-CZ) is active. The regex [[.ch.]]emie matches chemie. Notice the double square brackets. One pair for the bracket expression, and one pair for the collating sequence.

Other than POSIX-compliant engines part of a POSIX-compliant system, none of the regex flavors discussed in this tutorial support collating sequences.

Note that a fully POSIX-compliant regex engine treats ch as a single character when the locale is set to Czech. This means that [^x]emie also matches chemie. [^x] matches a single character that is not an x, which includes ch in the Czech POSIX locale.

In any other regular expression engine, or in a POSIX engine using a locale that does not treat ch as a digraph, [^x]emie matches the misspelled word cemie but not chemie, as [^x] cannot match the two characters ch.

Finally, note that not all regex engines claiming to implement POSIX regular expressions actually have full support for collating sequences. Sometimes, these engines use the regular expression syntax defined by POSIX, but don’t have full locale support. You may want to try the above matches to see if the engine you’re using does. Tcl’s regexp command, for example, supports the syntax for collating sequences. But Tcl only supports the Unicode locale, which does not define any collating sequences. The result is that in Tcl, a collating sequence specifying a single character matches just that character. All other collating sequences result in an error.

Character Equivalents

A POSIX locale can define character equivalents that indicate that certain characters should be considered as identical for sorting. In French, for example, accents are ignored when ordering words. élève comes before être which comes before événement. é and ê are all the same as e, but l comes before t which comes before v. With the locale set to French, a POSIX-compliant regular expression engine matches e, é, è and ê when you use the collating sequence [=e=] in the bracket expression [[=e=]].

If a character does not have any equivalents, the character equivalence token simply reverts to the character itself. [[=x=][=z=]], for example, is the same as [xz] in the French locale.

Like collating sequences, POSIX character equivalents are not available in any regex engine discussed in this tutorial, other than those following the POSIX standard. And those that do may not have the necessary POSIX locale support. Here too Tcl’s regexp command supports the syntax for character equivalents. But the Unicode locale, the only one Tcl supports, does not define any character equivalents. This effectively means that [[=e=]] and [e] are exactly the same in Tcl, and only match e, for any character you may try instead of “e”.

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