Search code examples
pythonregexfindall

Python - re.findall returns unwanted result


re.findall("(100|[0-9][0-9]|[0-9])%", "89%")

This returns only result [89] and I need to return the whole 89%. Any ideas how to do it please?


Solution

  • 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%']