Extract digits from a string within a word

I want a regular expression, which returns only digits, which are within a word, but I can only find expressions, which returns all digits in a string.

I’ve used this example:
text = 'I need this number inside my wor5d, but also this word3 and this 4word, but not this 1 and not this 555.'

The following code returns all digits, but I am only interested in [‘5’, ‘3’, ‘4’]
import re print(re.findall(r'\d+', text))

Any suggestions?

>Solution :

You can use

re.findall(r'(?<=[a-zA-Z])\d+|\d+(?=[a-zA-Z])', text)

This regex will extract all one or more digit chunks that are immediately preceded or followed with an ASCII letter.

A fully Unicode version for Python re would look like

(?<=[^\W\d_])\d+|\d+(?=[^\W\d_])

where [^\W\d_] matches any Unicode letter.

See the regex demo for reference.

Leave a Reply