Brandon
Brandon

Reputation: 97

regex: match number sequences without matching previous matches

I am looking through number sequences of 3 comma-delimited values and want to search for any sequence of 1,2,3. I want to match 1,2,3; 3,2,1; 2,1,3; etc. I do NOT want to match 1,1,1; 1,2,2; 1,3,3; 3,3,1; 2,3,3; using regexr.com for my regex parsing.

[123],[123],[123]

is what I started with until I realized it matched any character and not sequence of characters.

I was researching positive/negative lookaheads but could not think of how to structure it logically so the regex would not match a previously matched number in the specified sequence.

What fundamental thing am I missing here?

Upvotes: 4

Views: 119

Answers (2)

Brandon
Brandon

Reputation: 97

Answer#1 is @anubhava's solution, his solution correctly matches any sequence as long as all 3 integers are unique. However, in a situation where the sequence to search for has 2 repeated integers, you use the following regex, assuming your sequence to search for is 1,2,2. Can't believe I made it this hard :P

((1,2,2)|(2,1,2)|(2,2,1))

I realised that in a situation of 2 repeated integers, only 3 possible matches are available. So, instead of trying to build a complex lookbehind/lookahead regex we can simply search for those three occurrences literally. Using the capture groups should tag what it matched.

Obviously, in a sequence of 3 repeated integers such as 3,3,3 there is only one possible match so you search for it literally.

Upvotes: 0

anubhava
anubhava

Reputation: 784998

You can use a lookahead and back-reference based regex:

([123]),((?!\1)[123]),((?!\1|\2)[123])

RegEx Demo

RegEx Breakup:

([123])          # match 1 or 2 or 3 and capture it in group #1
,                # match literal comma
((?!\1)[123])    # match 1 or 2 or 3 if it is NOT same as group #1 & capture it in group #2
,                # match literal comma
((?!\1|\2)[123]) # match 1 or 2 or 3 if it is NOT same as group #1 OR #2

Upvotes: 6

Related Questions