Python - re.findall returns unwanted result
Solution 1:
>>> re.findall("(?:100|[0-9][0-9]|[0-9])%", "89%")
['89%']
When there are capture groups findall
returns only the captured parts. Use ?:
to prevent the parentheses from being a capture group.
Solution 2:
The trivial solution:
>>> re.findall("(100%|[0-9][0-9]%|[0-9]%)","89%")
['89%']
More beautiful solution:
>>> re.findall("(100%|[0-9]{1,2}%)","89%")
['89%']
The prettiest solution:
>>> re.findall("(?:100|[0-9]{1,2})%","89%")
['89%']
Solution 3:
Use an outer group, with the inner group a non-capturing group:
>>> re.findall("((?:100|[0-9][0-9]|[0-9])%)","89%")
['89%']