+ 2

How regular expressions work in python ?

I'm studying regular expressions in python 3 and I tried the following code : https://code.sololearn.com/ccJb19pWb4n8/?ref=app My problem is that I get an unexpected result. The objective is to obtain all the strings that match the pattern (ha){3,5).

21st Aug 2018, 3:21 PM
Ahmed Errami
Ahmed Errami - avatar
3 Answers
+ 6
You just need to change (ha) to (?:ha). => haRegex=re.compile(r'(?:ha){3,5}') output: ['hahaha', 'hahahaha', 'hahahahaha', 'hahahahaha'] (ha) creates a capturing group and will always find/return "ha" and nothing else. (?:ha) is a non-capturing group. It allows you to use brackets (), followed by quantifiers like {3,5}, without creating a capturing group. It's difficult to explain, but once you get it, it makes sense.
21st Aug 2018, 4:01 PM
Anna
Anna - avatar
+ 3
Ahmed Errami It will match the last string too because 'hahahahahaha' (6 x ha) contains 'hahahahaha' (5 x ha). If you don't want to include the last match, you can change the pattern to haRegex=re.compile(r'\b(?:ha){3,5}\b') \b marks a word boundary. Now it will only find whole "words" consisting of exactly 3 to 5 x ha: ['hahaha', 'hahahaha', 'hahahahaha'].
21st Aug 2018, 4:37 PM
Anna
Anna - avatar
+ 1
Thank you Anna. But I expect to get a list of only 3 elements because I have selected strings that contain 3 to 5 'ha' and they are 3.
21st Aug 2018, 4:27 PM
Ahmed Errami
Ahmed Errami - avatar