regex find lines with two sets of duplicates

Viewed 30

using regex is it possible to only find lines with two sets up duplicates such as

agaig (two a/g)
aagia (ignore this line)
aaigg (two a/g)

using the current regex

/(.){2}(?=.*?\1)

it highlights the 'aagia' and I don't want that line only lines with two sets of duplicates

1 Answers

You can use

^(?=.*(.).*?\1)(?=.*(?!\1)(.).*?\2).*

See the regex demo.

Details:

  • ^ - start of string
  • (?=.*(.).*?\1) - there must be (not necessarily consecutive) two repeated chars in the string ((.) captures a single char into Group 1)
  • (?=.*(?!\1)(.).*?\2) - there must be (not necessarily consecutive) two repeated chars in the string that are not identical to the char captured in Group 1
  • .* - the rest of the line.
Related