balajeerc
balajeerc

Reputation: 4098

Regular Expression: Consecutive Repetitions with a Letter In Between

I am trying to solve a problem with regular expressions. I am using Java regex which is similar to that in Perl apparently.

I want to search for a string that has a single digit from 1-9, repeating 3 or more times consecutively or with the single letter 'w' between repetitions.

For eg. 12333, 123w3, 12ww3, ww123 should all give successful matches but not 12345, 1234w, 1323w.

I tried the pattern "[[0-9]w]{3,}" but I now understand why that is definitely incorrect. Can someone give me a few clues to constructing the search pattern that fits my requirements?

Upvotes: 2

Views: 654

Answers (1)

Billy Moon
Billy Moon

Reputation: 58531

If I understand correctly that w is a wildcard (because I am not sure from what you have written - more by the examples you give) then this regex should work...

([1-9])(\1|w){2,}|w([1-9])(\3|w)+|ww([1-9])\5*

This is maybe not the most elegant solution, but should work, and breaks into parts like this...

           # matches that don't start with wildcards
([1-9])    # get a digit, and capture for future reference
(\1|w){2,} # the captured digit, or the letter w, 2 or more times
|          # or, matches that start with a single w
w          # match a w
([1-9])    # get a digit, and capture for future reference
(\3|w)+    # get one or more of the captured digit, or letter w
|          # or, matches starting with two w
ww         # get two w
([1-9])    # get a digit, and capture for future reference
\5*        # get all the successive captured digits

This should also work...

([1-9])(\1|w){2,}|w([0-9](\3|w)|w[0-9])

Upvotes: 4

Related Questions