Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex findall matches letters "a" to "z" in a string followed by another character

Result should return list which contains letter a to c followed by another character

Text is aabbcc I would expect it return ['a', 'a', 'b', 'b', 'c']

import re
text = 'aabbcc'
result = re.findall(r'([a-c]).', text)
print(result)

but it returns ['a', 'b', 'c']

like image 873
Sanatkumar Wagh Avatar asked Jan 23 '26 21:01

Sanatkumar Wagh


1 Answers

Use a non-capturing lookahead ([a-z](?=.)). This is basically the same as what you had, but doesn't capture the next character.

like image 115
user Avatar answered Jan 25 '26 12:01

user