1

This is a generic regular expression question, that I couldn't find an answer too.

Lets say I have a few input strings:

...A-A-B...
...B-A-A...
...A-B-A...
...B-B-A...

I have two delimiters A and B. What I want to do is find either of these, and then look for the other one. Now I know I can do (A.*B)|(B.*A) which will match what I want for all four strings.

And I can also do (A|B).*\1 which would match A-A for the first and second strings, A-B-A for the third and B-B for the last.

Can I do something alongs the lines of (A|B).*[^\1] to say, find one of two tokens then find the other token?

For context, I was interested in this because of this question, where A and B are a dot or comma for currency matching, but I figure this could be useful in lots of contexts, hence the generic question.

Community
  • 1
  • 1

2 Answers2

3

Negative lookahead could do it.

(A|B).*(?!\1)(A|B)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
3

You can use a conditional:

(?:(A)|B).*?(?(1)B|A)

(?(1)B|A) is an IF..THEN..ELSE and means: if group 1 then B else A

Notice: this doesn't work with java, javascript and GO but works with Perl, PHP, Python, Ruby, .net

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125