Home > Mobile >  Python Regex Return Number After Space or Hyphen and Exclude Single Digit
Python Regex Return Number After Space or Hyphen and Exclude Single Digit

Time:09-18

I have the following code:

text = str('''\
num1 - 1.23
num2-3.21
num3 - 9.31\
''')

re.findall(r'[\d.] ', str([i for i in text.split('\n')]))

Please note how num2 did not have any spaces.

This returns:

['1', '1.23', '2', '3.21', '3', '9.31']

However I only want to return a list of the value of each num.

['1.23', '3.21', '9.31']

Does anyone know how I can ignore the first digit attached to num and only retrieve the digits after a space or a hyphen?

CodePudding user response:

You can search and learn about lookahead and lookbehind in the context of regular expressions.

only retrieve the digits after a space or a hyphen?

re.findall(r'(?<=[ -])\d (?:\.\d )?', text)
  • (?<=[ -]) means: match only if it is preceded by space or hyphen, but do not capture.
  • \d means: match one or more digits
  • (?:\.\d )? means: optionally, match if followed by a dot with one or more digits, and capture.

CodePudding user response:

If you are using re.findall you can use a capture group.

To prevent a partial word match at the end of the digits, you could add a word boundary \b if desired.

[ -](\d (?:\.\d )?)

Explanation

  • [ -] Match either a space or hyphen
  • ( Capture group 1
    • \d (?:\.\d )? Match 1 digits with an optional decimal part
  • ) Close group 1

Regex demo

import re

text = ("num1 - 1.23\nnum2-3.21\nnum3 - 9.31")
print(re.findall(r'[ -](\d (?:\.\d )?)', text))

Output

['1.23', '3.21', '9.31']
  • Related