Mrc0113
Mrc0113

Reputation: 556

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

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"

Upvotes: 3

Views: 2455

Answers (1)

Martin Ender
Martin Ender

Reputation: 44259

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

Upvotes: 6

Related Questions