I am new to python and trying to solve some problems (in the way to learn).
I want to match space-separated words that contain two or fewer o characters.
That is what I actually did:
import re
pattern = r'\b(?:[^a\s]*o){1}[^a\s]*\b'
text = "hop hoop hooop hoooop hooooop"
print(re.findall(pattern, text))
When I run my code it does match all the words in the string..
Any suggestion?
CodePudding user response:
You can use
import re
pattern = r'(?<!\S)(?:[^\so]*o){0,2}[^o\s]*(?!\S)'
text = "hop hoop hooop hoooop hooooop"
print(re.findall(pattern, text))
# Non regx solution:
print([x for x in text.split() if x.count("o") < 3])
See the Python demo. Both yield ['hop', 'hoop']
.
The (?<!\S)(?:[^\so]*o){0,2}[^o\s]*(?!\S)
regex matches
(?<!\S)
- a left-hand whitespace boundary(?:[^\so]*o){0,2}
- zero, one or two occurrences of any zero or more chars other than whitespace ando
char, and then ano
char[^o\s]*
- zero or more chars other thano
and whitespace(?!\S)
- a right-hand whitespace boundary