Home > Blockchain >  How to pick out a particular group of numbers in a string
How to pick out a particular group of numbers in a string

Time:12-04

I have a string (which is different everytime but follows a similar format):

54321.0 67778.0000002 0.3673987009 1 1017.750 88.370 512 7 1 H8711-3040

I'm trying to use regular expressions to be able to pick out any one of those entries in my python code, but I'm struggling.

I'm trying

testRegex = re.compile(r'\w. \w')
out = testRegex.search('# 53421.0 67778.0000002 0.3673987009 1 1517.750 88.370 512 7 1 J1745-3040')
print(out.group())

But I'm just getting the whole line returned. I can see why it does that but not sure how to just pick out "512" for example (whatever number might be in its place).

CodePudding user response:

You can use

re.findall(r'\b\w(?:\S*\w)?\b', s) 

See the regex demo. Details:

  • \b - a word boundary
  • \w - a word char
  • ?:\S*\w)? - an optional sequence of any zero or more non-whitespace chars and then a word char
  • \b - a word boundary

See the Python demo:

import re
s = '# 53421.0 67778.0000002 0.3673987009 1 1517.750 88.370 512 7 1 J1745-3040'
print( re.findall(r'\b\w(?:\S*\w)?\b', s) )
# => ['53421.0', '67778.0000002', '0.3673987009', '1', '1517.750', '88.370', '512', '7', '1', 'J1745-3040']
  • Related