适用于3位数条件的有效java正则表达式

适用于3位数条件的有效java正则表达式,java,regex,Java,Regex,除了“不应匹配”列表中的示例外,我正在尝试分别匹配3位数字 我在下面展示的当前正则表达式不能完全工作,我不知道如何为所有用例调整它 当前Java正则表达式: (^.*err.*[a-z].*$)|(^\d{3}$)|(^.*\d{3}\s\b$) The below items should match: ----------------------------- 123 Match the number in this sentence 123 as well 999 The below

除了“不应匹配”列表中的示例外,我正在尝试分别匹配3位数字

我在下面展示的当前正则表达式不能完全工作,我不知道如何为所有用例调整它

当前Java正则表达式:

(^.*err.*[a-z].*$)|(^\d{3}$)|(^.*\d{3}\s\b$)
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.*[a-z].*$)|(^\d{3}$)|(^.*\d{3}\s\b$)
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
(?:(?)

试试正则表达式

正如Wiktor所指出的,它将与末尾的
920
匹配

另外请注意,我使用了不区分大小写的搜索

正则表达式解释

(?:      #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
(err\s*?\d{1})   #Group One thus exhausts matching any further and thus discarded.
\s+(\d{3})\b     #Matches all 3 digit groups occurring solely.
工作javascript版本


进一步编辑

(err\s*?\d{1})|[^\d]\s+(\d{3})(?:$|\s+(?!\d))
试试正则表达式

Java脚本

产出将是:-

123
123
999

你的问题是什么?@luk2302我已经调整了问题
920 1234 1234
有什么问题?
920
在这里似乎是一个有效的匹配项。@WiktorStribiżew它只需要匹配3位数字,而不是当有其他数字sacc时。对于这个要求,
456 123
应该失败。字符串中有多个数字。另外,你说正则表达式也应该在JavaScript中工作?这太宽泛了。谢谢。但它需要与Java(或JavaScript)相匹配正则表达式,这是一个python匹配。@0x616f在我看来像一个普通的Java正则表达式。为什么你认为它不是?但它确实看起来会匹配,即使在同一行上有更多的数字,这不应该是这样。好的@0x616f,javascript后面没有负面的外观…让我看看。这是我检查的第一件事,javascript没有ag不见了。现在我看到你已经编辑了这个问题。让我试一试