字符类别交集字符类别交集字符类别交集字符类别交集
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

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

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

字符类别交集

Java 和 Ruby 1.9 及更新版本支持字符类别交集。这可以轻松配对必须存在于两个字符组中的任何单一字符。语法为 [类别&&[交集]]。您可以在交集的字符类别中使用完整的字符类别语法。

如果交集类别不需要否定插入符号,则 Java 和 Ruby 允许您省略嵌套方括号:[类别&&交集]。

字符类别 [a-z&&[^aeiuo]] 符合单一非元音字母。换句话说:它符合单一辅音字母。在没有字符类别 减法 或交集的情况下,运行此项操作的唯一方法是列出所有辅音字母:[b-df-hj-np-tv-z]。

字符类别 [\p{Nd}&&[\p{IsThai}]] 符合任何单一泰文数字。 [\p{IsThai}&&[\p{Nd}]] 完全运行相同的动作。

多个类别的交集

您可以多次交集同一个类别。[0-9&&[0-6&&[4-9]]] 与 [4-6] 相同,因为这些是交集所有三个部分中唯一出现的数字。在 Java 和 Ruby 中,您可以将相同的正则表达式写成 [0-9&&[0-6]&&[4-9]]、[0-9&&[0-6&&4-9]]、[0-9&&0-6&&[4-9]],或仅 [0-9&&0-6&&4-9]。仅当交集的一部分被否定时,才需要嵌套方括号。

如果您没有在交集的右半部分周围使用方括号,那么不会混淆字符类别的整个剩余部分就是交集的右半部分。如果您确实使用方括号,您可以写一些像 [0-9&&[12]56]。在 Ruby 中,这与 [0-9&&1256] 相同。但 Java 有错误,导致它将其视为 [0-9&&56],完全忽略嵌套括号。

字符 56 在 [0-9&&[12]56] 中是一个错误。这样它们的含义就没有歧义了。

您也不应将 && 放在正则表达式的开头或结尾。Ruby 将 [0-9&&] 和 [&&0-9] 视为与空类别的交集,这完全不符合任何字符。Java 会忽略前导和尾随的 && 操作符。

否定类别中的交集

字符类别 [^1234&&[3456]] 同时被否定且相交。在 Java 中,否定优先于交集。Java 将此正则表达式读取为「(非 1234)和 3456」。因此,在 Java 中,此类别与 [56] 相同,并符合数字 5 和 6。在 Ruby 中,交集优先于否定。Ruby 将 [^1234&&3456] 读取为「非(1234 和 3456)」。因此,在 Ruby 中,此类别与 [^34] 相同,符合除了数字 3 和 4 之外的任何内容。

如果您想要否定交集的右手边,则必须使用方括号。它们会自动控制优先级。因此,Java 和 Ruby 都会将 [1234&&[^3456]] 读取为「1234 和(非 3456)」。因此,此正则表达式与 [12] 相同。

与其他正则表达式风格的符号兼容性

在本文讨论的任何其他正则表达式风格中,字符类别中的缩写符号没有特殊含义。缩写符号只是一个文本,重复它只会增加不必要的重复。所有这些风格都将 [1234&&3456] 视为与 [&123456] 相同。

严格来说,这表示字符类别交集语法与大多数其他正则表达式不符。但在实务上没有差别,因为当你只想加入一个字面上的与号时,在字符类别中使用两个与号没有意义。Java 和 Ruby 将单一与号视为字面。

字元類別交集
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » 字元類別交集

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

字元類別交集

Java 和 Ruby 1.9 及更新版本支援字元類別交集。這可以輕鬆配對必須存在於兩個字元組中的任何單一字元。語法為 [類別&&[交集]]。您可以在交集的字元類別中使用完整的字元類別語法。

如果交集類別不需要否定插入符號,則 Java 和 Ruby 允許您省略巢狀方括號:[類別&&交集]。

字元類別 [a-z&&[^aeiuo]] 符合單一非母音字母。換句話說:它符合單一子音字母。在沒有字元類別 減法 或交集的情況下,執行此項操作的唯一方法是列出所有子音字母:[b-df-hj-np-tv-z]。

字元類別 [\p{Nd}&&[\p{IsThai}]] 符合任何單一泰文數字。 [\p{IsThai}&&[\p{Nd}]] 完全執行相同的動作。

多個類別的交集

您可以多次交集同一個類別。[0-9&&[0-6&&[4-9]]] 與 [4-6] 相同,因為這些是交集所有三個部分中唯一出現的數字。在 Java 和 Ruby 中,您可以將相同的正規表示式寫成 [0-9&&[0-6]&&[4-9]]、[0-9&&[0-6&&4-9]]、[0-9&&0-6&&[4-9]],或僅 [0-9&&0-6&&4-9]。僅當交集的一部分被否定時,才需要嵌套方括號。

如果您沒有在交集的右半部分周圍使用方括號,那麼不會混淆字元類別的整個剩餘部分就是交集的右半部分。如果您確實使用方括號,您可以寫一些像 [0-9&&[12]56]。在 Ruby 中,這與 [0-9&&1256] 相同。但 Java 有錯誤,導致它將其視為 [0-9&&56],完全忽略嵌套括號。

字元 56 在 [0-9&&[12]56] 中是一個錯誤。這樣它們的含義就沒有歧義了。

您也不應將 && 放在正規表示式的開頭或結尾。Ruby 將 [0-9&&] 和 [&&0-9] 視為與空類別的交集,這完全不符合任何字元。Java 會忽略前導和尾隨的 && 運算子。

否定類別中的交集

字元類別 [^1234&&[3456]] 同時被否定且相交。在 Java 中,否定優先於交集。Java 將此正規表示式讀取為「(非 1234)和 3456」。因此,在 Java 中,此類別與 [56] 相同,並符合數字 5 和 6。在 Ruby 中,交集優先於否定。Ruby 將 [^1234&&3456] 讀取為「非(1234 和 3456)」。因此,在 Ruby 中,此類別與 [^34] 相同,符合除了數字 3 和 4 之外的任何內容。

如果您想要否定交集的右手邊,則必須使用方括號。它們會自動控制優先順序。因此,Java 和 Ruby 都會將 [1234&&[^3456]] 讀取為「1234 和(非 3456)」。因此,此正規表示式與 [12] 相同。

與其他正規表示式風味的符號相容性

在本文討論的任何其他正規表示式風味中,字元類別中的縮寫符號沒有特殊含義。縮寫符號只是一個文字,重複它只會增加不必要的重複。所有這些風味都將 [1234&&3456] 視為與 [&123456] 相同。

嚴格來說,這表示字元類別交集語法與大多數其他正規表示法不符。但在實務上沒有差別,因為當你只想加入一個字面上的與號時,在字元類別中使用兩個與號沒有意義。Java 和 Ruby 將單一與號視為字面。

Character Class Intersection
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » Character Class Intersection

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

Character Class Intersection

Character class intersection is supported by Java, and by Ruby 1.9 and later. It makes it easy to match any single character that must be present in two sets of characters. The syntax for this is [class&&[intersect]]. You can use the full character class syntax within the intersected character class.

If the intersected class does not need a negating caret, then Java and Ruby allow you to omit the nested square brackets: [class&&intersect].

The character class [a-z&&[^aeiuo]] matches a single letter that is not a vowel. In other words: it matches a single consonant. Without character class subtraction or intersection, the only way to do this would be to list all consonants: [b-df-hj-np-tv-z].

The character class [\p{Nd}&&[\p{IsThai}]] matches any single Thai digit. [\p{IsThai}&&[\p{Nd}]] does exactly the same.

Intersection of Multiple Classes

You can intersect the same class more than once. [0-9&&[0-6&&[4-9]]] is the same as [4-6] as those are the only digits present in all three parts of the intersection. In Java and Ruby you can write the same regex as [0-9&&[0-6]&&[4-9]], [0-9&&[0-6&&4-9]], [0-9&&0-6&&[4-9]], or just [0-9&&0-6&&4-9]. The nested square brackets are only needed if one of the parts of the intersection is negated.

If you do not use square brackets around the right hand part of the intersection, then there is no confusion that the entire remainder of the character class is the right hand part of the intersection. If you do use the square brackets, you could write something like [0-9&&[12]56]. In Ruby, this is the same as [0-9&&1256]. But Java has bugs that cause it to treat this as [0-9&&56], completely ignoring the nested brackets.

The characters 56 in [0-9&&[12]56] are an error. This way there is no ambiguity about their meaning.

You also shouldn’t put && at the very start or very end of the regex. Ruby treats [0-9&&] and [&&0-9] as intersections with an empty class, which matches no characters at all. Java ignores leading and trailing && operators.

Intersection in Negated Classes

The character class [^1234&&[3456]] is both negated and intersected. In Java, negation takes precedence over intersection. Java reads this regex as “(not 1234) and 3456”. Thus in Java this class is the same as [56] and matches the digits 5 and 6. In Ruby, intersection takes precedence over negation. Ruby reads [^1234&&3456] as “not (1234 and 3456)”. Thus in Ruby this class is the same as [^34] which matches anything except the digits 3 and 4.

If you want to negate the right hand side of the intersection, then you must use square brackets. Those automatically control precedence. So Java and Ruby all read [1234&&[^3456]] as “1234 and (not 3456)”. Thus this regex is the same as [12].

Notational Compatibility with Other Regex Flavors

The ampersand has no special meaning in character classes in any other regular expression flavors discussed in this tutorial. The ampersand is simply a literal, and repeating it just adds needless duplicates. All these flavors treat [1234&&3456] as identical to [&123456].

Strictly speaking, this means that the character class intersection syntax is incompatible with the majority of other regex flavors. But in practice there’s no difference, because there is no point in using two ampersands in a character class when you just want to add a literal ampersand. A single ampersand is still treated as a literal by Java and Ruby.

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