How would I match the following strings:
str1 = "he will be 60 years old today"
str2 = "she turns 79yo today this afternoon"
I want to match strings that contain a digit or digit immediately followed by characters (no whitespace separated).
You can use this regex to match those words:
\b\d+\w*\b
RegEx Demo
Code:
import re
p = re.compile(ur'\b\d+\w*\b')
test_str = u"he will be 60 years old today\nshe turns 79yo today this afternoon"
print re.findall(p, test_str)
Output:
[u'60', u'79yo']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With