0

An example of a matching line is: Gutter, Rubbing, Nomad, Among, Nit, Turret, Tug

This line contains 3 words that all have 2 consecutive identical letters.

I'm currently using the following regex expression with the grep -P command: (?:.*(?:(\w)\1).*){3}.

This regex works but when I have the following line for example: Gutter, Rutting, Nomad, Among, Nit, Turret, Tugekekekekekekekekekekeke I get a catastrophic backtracking error on regex101.com (https://regex101.com/r/3YFCAj/1).

How can I search for this regex without that backtracking problem?

1
  • You don't need that first .*, which is what's causing the backtracking error on that site.
    – Kusalananda
    Commented Nov 9, 2022 at 12:06

1 Answer 1

1

If you want to use Perl style regex, that should work:

(\b\w*(\w)\2\w*\b.*){3}

Explanation:

  • (\w)\2 will match two identical consecutive characters. The \2 will match the second matching group, which is the (\w) that precedes it.
  • \w*(\w)\2\w* those characters should be inside a word with other word characters before and/or after.
  • \b\w*(\w)\2\w*\b - and those words should be surrounded by word boundaries.
  • .* - after each word we can have any other characters.
  • (\b\w*(\w)\2\w*\b.*){3} - and finally this part should repeat 3 times to find at least 3 of those words.
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .