Home > Net >  Break at spaces and select string contianing numbers
Break at spaces and select string contianing numbers

Time:01-21

In python, I want a to convert this with regex

"Something 19.28 else" -> "19.28"

"Someone 18.16-one-0 other" -> "18.16-one-0"

It means, break at whitespaces and select the one containing numbers.

CodePudding user response:

Something like this seems to fit your question:

foo = 'foo 19.29 bar'
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

terms = foo.split()
converted = None

for i in terms:
    for j in numbers:
        if j in i:
            converted = i
            break

print(converted)

Hope this helps!

CodePudding user response:

you can simply split the string and search for numbers in the string

import re
text = "Someone 18.16-one-0 other"

print(''.join([i for i in text.split() if re.search(r'\d', i)]))

>>> 18.16-one-0
  • Related