-2

I am trying to match 3 digit numbers individually except for the examples in the "should not match" list.

The current regex I have shown below does not work completely and I am not sure how to adjust it for all use cases.

Current Java Regex:

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

Test strings:

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
AO_
  • 2,573
  • 3
  • 30
  • 31

1 Answers1

0

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

Try the regex here.

As Wiktor pointed out it will match the 920 at the end.

Also please note that I've used a case insensitive search.

Regex Explain

(?:      #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

EDITED Changing the answer based on requirement for javascript

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


Try regex here

Matches required are from group 2 only.

Regex Explain

(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.

Working javascript version here.


EDITED FURTHER

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

Try the regex here

Java script here

Output would be:-

123
123
999
kaza
  • 2,317
  • 1
  • 16
  • 25
  • Thanks. But it needs to match for Java (or javascript) regex, this is a python match. – AO_ Sep 22 '17 at 09:44
  • @0x616f Looks like a normal Java regex to me. Why do you think it is not? But it does look like it will match even if there are more numbers on the same line, which should not be the case. – Erwin Bolwidt Sep 22 '17 at 09:47
  • Okay @0x616f, javascript doesn't have a negative look-behind... Let me see. That's the first thing I checked, and the javascript tag is missing. – kaza Sep 22 '17 at 09:49
  • Now I see you've edited the question. Let me give it a try – kaza Sep 22 '17 at 10:13