-1

I have to match sequence like baab, sdds in the string and the sequence should not be inside the square brackets. So far i have used the regular expression

/([a-z])([a-z])\2{1}\1{1}(?![^[]*])/

It is giving me the desired output but the above regular expression also matching the sequence like bbbb which is undesirable. I am thinking of having something to compare capture group1 and capture group2 not be equal.So far i didn't find anything useful.

Link for same

ashofphoenix
  • 123
  • 1
  • 6

1 Answers1

2

I think you could add a negative lookahead after group 1 (?!\1) to assert that what follows the first group is not the same as group 1.

([a-z])(?!\1)([a-z])\2\1(?![^[]*])

If you don't want your match to be between square brackets, you could add a negative lookbehind (?<!\[)([a-z])(?!\1)([a-z])\2\1(?!]) to assert that your match does not start with [. (if your engine supports this).

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • the regex given in line 1 is does ignoring the match if inside square brackets. I was wondering if it is possible to ignore the match found in line if there also exist a match between square brackets – ashofphoenix Jan 21 '18 at 14:31
  • @ashofphoenix Not really sure what you mean, could you give an example? – The fourth bird Jan 21 '18 at 14:52
  • Yes as you can see that in test input line 2 has the match inside as well as the outside of brackets https://regex101.com/r/Jqw6Pm/4 once i remove the pattern to ignore square brackets – ashofphoenix Jan 21 '18 at 14:58
  • @ashofphoenix For this scenario with new requirements, you could consider creating a new question with a detailed example and if possible the programming language. – The fourth bird Jan 21 '18 at 15:45