1

I'm trying to get a regex that captures repeated letters except for the first letter that is matched.

I have a regex that gets the repeated letters: ([a-z])\1{2,}

However this would capture "ooo" in sooo. But i would only want to capture "oo"

(using this for the find and replace function in google sheets)

Mijawel
  • 389
  • 2
  • 15

1 Answers1

3

Since you didn't mention what programming language or regex flavor you're using, I will provide multiple options:

  1. Use a capturing group:

    ([a-z])(\1{2,})
    

    Demo.

    This will work with every regex flavor but your expected match would be in the second capturing group.

  2. If your regex flavor supports Lookarounds, use a positive Lookbehind:

    (?<=([a-z]))\1{2,}
    

    Demo.

  3. If your regex flavor supports \K, you can use the following:

    ([a-z])\K\1{2,}
    

    Demo.

Note that \1{2,} means that you'll have a match only if the letter is repeated at least 3 times (e.g., aaa). If that's not what you intended and you want to have a match when the letter is repeated twice, you should use \1+ instead.

ICloneable
  • 613
  • 3
  • 17