tetris555
tetris555

Reputation: 315

Matching certain number of specific letter in a word

I am trying to match same ('reference') letter only in a word. For example:

Makaraka

Wasagara

degenerescence

desilicification

odontonosology

There are 4 'a' in the first word, 6 'o' in the last one. How can I match all of then using RE? I tried using backreference, but I couldn't manage, the last "sample" letter was never matched. Is there a way to specify the number of occurrences for a capturing group? Thanks.

Upvotes: 0

Views: 99

Answers (2)

anubhava
anubhava

Reputation: 784998

You can use this regex:

^.*?(\w)(?=(?:.*?\1){3}).*$

RegEx Demo

Explanation: This regex matches any word character in the input and captures it for back reference \1 later. Then the lookahead part (?=(?:.*?\1){3}) ensures that there are at least 3 more occurrences of the captures word character.

Upvotes: 1

Toto
Toto

Reputation: 91385

How about:

(?:.*a){4,}

Just change the a for the letter you're searching.

Upvotes: 0

Related Questions