Sam S.
Sam S.

Reputation: 153

Python find double letters

I'm trying to get all 3 letter words. They end with double letters and start with the letter 'a'.

Like: app, add, all, arr, aoo, aee

I tried this but It doesn't work very well...

words =re.findall(r" a(\w)\1* ",text)

Upvotes: 0

Views: 3029

Answers (2)

Shawn Mehan
Shawn Mehan

Reputation: 4578

You are using

words =re.findall(r" a(\w)\1* ",text)

and here is a demo of it.

You can see an improvement by using a word boundary and as well as a specific limit of matches in your search here

\ba(\w)\1{1}\b

as you want 1 and only 1 additional instances of the matched \w, achieved with the {1} which only allows 1 of the match, i.e., \1 which is an additional \w.

Upvotes: 4

Inverse
Inverse

Reputation: 4476

I think you need to use + instead of *:

words = re.findall(r"\ba(\w)\1+\b", text)

Otherwise you will match things with non-double letters. Also use \b to detect word boundaries.

Upvotes: 3

Related Questions