如何在正则表达式中制作 OR 运算符?
How to make OR operator in regular expression?
我想要的是这样的:
(
[integer OR (ANY but not integer or white-space)]
[(ONE white-space OR NONE)]
[integer OR (ANY but not integer or white-space)]
)
将匹配的字符串示例:99 $
99$
$ 99
</code></p>
<p>我现在有两个正则表达式:</p>
<p><code>^[^\d\s](\s{0,1})\d+
和 ^\d+(\s{0,1})[^\d\s]
关于如何仅用一个正则表达式替换这两个的想法?
在正则表达式中创建 or 的标准方法是竖线 |
(ref)
如果您需要匹配正则表达式中的一个或另一个,则需要将 (A|B)
与 A=^[^\d\s](\s{0,1})\d+
和 B=^\d+(\s{0,1})[^\d\s]
匹配
结果是^(([^\d\s](\s{0,1})\d+)|(\d+(\s{0,1})[^\d\s]))
"|"是正则表达式中的 "or" 等价物。将它们分组并放置 |介于两者之间。
[^\d\s](\s{0,1})\d+|^\d+(\s{0,1})[^\d\s]
您的示例效果很好。
^((?:\d+\s?[^\d\s]+)|(?:[^\d\s]+\s?\d+))$
尝试 this.See 演示。
我想要的是这样的:
(
[integer OR (ANY but not integer or white-space)]
[(ONE white-space OR NONE)]
[integer OR (ANY but not integer or white-space)]
)
将匹配的字符串示例:99 $
99$
$ 99
</code></p>
<p>我现在有两个正则表达式:</p>
<p><code>^[^\d\s](\s{0,1})\d+
和 ^\d+(\s{0,1})[^\d\s]
关于如何仅用一个正则表达式替换这两个的想法?
在正则表达式中创建 or 的标准方法是竖线 |
(ref)
如果您需要匹配正则表达式中的一个或另一个,则需要将 (A|B)
与 A=^[^\d\s](\s{0,1})\d+
和 B=^\d+(\s{0,1})[^\d\s]
结果是^(([^\d\s](\s{0,1})\d+)|(\d+(\s{0,1})[^\d\s]))
"|"是正则表达式中的 "or" 等价物。将它们分组并放置 |介于两者之间。
[^\d\s](\s{0,1})\d+|^\d+(\s{0,1})[^\d\s]
您的示例效果很好。
^((?:\d+\s?[^\d\s]+)|(?:[^\d\s]+\s?\d+))$
尝试 this.See 演示。