I want to find words that have consecutive letter pairs using regex.
I know for just one consecutive pair like zoo (oo), puzzle (zz), arrange (rr), it can be achieved by '(\w){2}'
. But how about
edit:
'(\w){2}'
is actually wrong, it finds any two letters instead of a double letter pair.Use re.finditer
>>> [m.group() for m in re.finditer(r'((\w)\2)+', 'zoo')]
['oo']
>>> [m.group() for m in re.finditer(r'((\w)\2)+', 'arrange')]
['rr']
>>> [m.group() for m in re.finditer(r'((\w)\2)+', 'committee')]
['mm', 'ttee']
>>> [m.group() for m in re.finditer(r'((\w)\2)+', 'bookkeeper')]
['ookkee']
Check whether the string contain consecutive pair:
>>> bool(re.search(r'((\w)\2){2}', 'zoo'))
False
>>> bool(re.search(r'((\w)\2){2}', 'arrange'))
False
>>> bool(re.search(r'((\w)\2){2}', 'committee'))
True
>>> bool(re.search(r'((\w)\2){2}', 'bookkeeper'))
True
You can also use following non-capturing (?:
) version:
(?:(\w)\1){2}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With