有效的Java Regex,适用于3位数字条件



我试图单独匹配3位数字,除了"应该不应该匹配"列表中的示例。

我在下面显示的当前正则言语不完全工作,我不确定如何在所有用例中调整它。

当前的Java正则正则:

(^.*err.*[a-z].*$)|(^d{3}$)|(^.*d{3}sb$)

测试字符串:

The below items should match:
-----------------------------
123
Match the number in this sentence 123 as well
999
The below items should NOT match:
---------------------------------
1234
12345
123456
1234567
£123
$456
Err404
ERR404
err404
Err 404
ERR 404
err 404
there is err 404 on page
this err 1232222222222 as well
a string 12323 like this
asd
4444333322221111
4444 3333 2222 1111
Err123
02012341234
920 1234 1234

(?:(?<!err)s+?)(d{3})b

在此处尝试以下等级。

正如Wiktor指出的那样,它将与920匹配。

另外,请注意,我已经使用了案例不敏感的搜索。

REGEX解释

(?:      #Non Capturing group START
(?<!     #Negative look-behind don't match if preceded by this
err      #Shouldn't precede by err NOTE that we've to use case insensitive flag
)        #END Negative look behind
s+?     #Followed by multi optional space  
)        #End Non capturing group
(d{3})  #Match exactly 3 digits
b       #The 3 digits have end with a word boundary

编辑根据JavaScript的要求更改答案

(errs*?d{1})|s+(d{3})b


在这里尝试Regex

所需的匹配仅来自第2组。

REGEX解释

(errs*?d{1})   #Group One thus exhausts matching any further and thus discarded.
s+(d{3})b     #Matches all 3 digit groups occurring solely.

在此处工作JavaScript版本。


编辑进一步编辑

(errs*?d{1})|[^d]s+(d{3})(?:$|s+(?!d))

在此处尝试以下等级

Java脚本在这里

输出将是: -

123
123
999

最新更新