Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regular Expression for number of exactly 5 digits anywhere in the string

Tags:

java

regex

I'm trying to create a regular expression to parse a 5 digit number out of a string no matter where it is but I can't seem to figure out how to get the beginning and end cases.

I've used the pattern as follows \\d{5} but this will grab a subset of a larger number...however when I try to do something like \\D\\d{5}\\D it doesn't work for the end cases. I would appreciate any help here! Thanks!

For a few examples (55555 is what should be extracted):

At the beginning of the string
"55555blahblahblah123456677788"

In the middle of the string
"2345blahblah:55555blahblah"

At the end of the string
"1234567890blahblahblah55555"
like image 291
Mrc0113 Avatar asked Dec 04 '25 12:12

Mrc0113


1 Answers

Since you are using a language that supports them use negative lookarounds:

"(?<!\\d)\\d{5}(?!\\d)"

These will assert that your \\d{5} is neither preceded nor followed by a digit. Whether that is due to the edge of the string or a non-digit character does not matter.

Note that these assertions themselves are zero-width matches. So those characters will not actually be included in the match. That is why they are called lookbehind and lookahead. They just check what is there, without actually making it part of the match. This is another disadvantage of using \\D, which would include the non-digit character in your match (or require you to use capturing groups).

like image 103
Martin Ender Avatar answered Dec 06 '25 02:12

Martin Ender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!