Search code examples
pythonregexfindall

Extracting optional quantity of words


I have a string like this-

string="John has got 6 cats but I think my friend Susan has 3 dogs and Mike has 8 fishes"

I want to write regex to extract pattern Name verb after it and #of pets and what kind of pets it is.

re.findall('[A-Za-z]+ \w+ \d+ \w+', string)

works with Susan has 3 dogs, Mike has 8 fishes.

But it doesn't work with John has got 6 cats

how can I edit code to make it look for one or two words after name?

Thank you in advance!


Solution

  • You might use non-capturing group (?:...) and {1,2} to denote 1 to 2 repetition as follows

    import re
    string="John has got 6 cats but I think my friend Susan has 3 dogs and Mike has 8 fishes"
    found=re.findall(r'[A-Z][a-z]+ (?:\w+ ){1,2}\d+ \w+', string)
    print(found)
    

    output

    ['John has got 6 cats', 'Susan has 3 dogs', 'Mike has 8 fishes']