简体   繁体   English

正则表达式匹配一个或两个数字

[英]Regex match one digit or two

If this 如果这

(°[0-5])

matches °4 匹配°4

and this 还有这个

((°[0-5][0-9]))

matches °44 匹配°44

Why does this 为什么这样呢

((°[0-5])|(°[0-5][0-9]))

match °4 but not °44? 匹配°4但不是°44?

Because when you use logical OR in regex the regex engine returns the first match when it find a match with first part of regex (here °[0-5] ), and in this case since °[0-5] match °4 in °44 it returns °4 and doesn't continue to match the other case (here °[0-5][0-9] ): 因为当你在正则表达式中使用逻辑OR时,正则表达式引擎在找到与正则表达式的第一部分匹配时返回第一个匹配(此处为°[0-5] ),在这种情况下,因为°[0-5]匹配°4 in °44它返回°4并且不会继续匹配其他情况(此处为°[0-5][0-9] ):

((°[0-5])|(°[0-5][0-9]))

A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the '|' A | B,其中A和B可以是任意RE,创建一个与A或B匹配的正则表达式。任意数量的RE可以用'|'分隔 in this way. 通过这种方式。 This can be used inside groups (see below) as well. 这也可以在组内使用(见下文)。 As the target string is scanned, REs separated by '|' 扫描目标字符串时,RE由“|”分隔 are tried from left to right. 从左到右尝试。 When one pattern completely matches, that branch is accepted. 当一个模式完全匹配时,接受该分支。 This means that once A matches, B will not be tested further, even if it would produce a longer overall match. 这意味着一旦A匹配,B将不会被进一步测试,即使它会产生更长的整体匹配。 In other words, the '|' 换句话说,'|' operator is never greedy. 操作员从不贪心。 To match a literal '|', use \\|, or enclose it inside a character class, as in [|]. 要匹配文字“|”,请使用\\ |,或将其括在字符类中,如[|]中所示。

You are using shorter match first in regex alternation. 你在正则表达式交替中首先使用较短的匹配。 Better use this regex to match both strings: 更好地使用此正则表达式来匹配两个字符串:

°[0-5][0-9]?

RegEx Demo RegEx演示

Because the alternation operator | 因为交替运算符| tries the alternatives in the order specified and selects the first successful match. 按指定顺序尝试替代选项并选择第一个成功匹配。 The other alternatives will never be tried unless something later in the regular expression causes backtracking. 除非正则表达式中的某些内容导致回溯,否则将永远不会尝试其他替代方案。 For instance, this regular expression 例如,这个正则表达式

(a|ab|abc)

when fed this input: 当输入此输入时:

abcdefghi

will only ever match a . 只会匹配a However, if the regular expression is changed to 但是,如果正则表达式更改为

(a|ab|abc)d

It will match a . 它会匹配a Then since the next characyer is not d it backtracks and tries then next alternative, matching ab . 然后,因为下一个characyer不d它回溯,然后尝试下一个替换,匹配ab And since the next character is still not d it backtracks again and matches abc ...and since the next character is d , the match succeeds. 并且因为下一个字符仍然不是d它再次回溯并且匹配abc ...并且因为下一个字符是d ,所以匹配成功。

Why would you not reduce your regular expression from 你为什么不减少你的正则表达式

((°[0-5])|(°[0-5][0-9]))

to this? 这个?

°[0-5][0-9]?

It's simpler and easier to understand. 它更简单,更容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM