Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEX in Python only matches exponent

Tags:

python

regex

I was reading some lines from a file, which I want to match to be floats, here is a minimal example:

import re
regex="[-+]?[0-9]+\.?[0-9]+([eE][-+]?[0-9]+)?"
string="0.00000000000000000E0   0.00000000000000000E0   0.00000000000000000E0"´
print(re.findall(regex,string))

, Which gives me

['E0', 'E0', 'E0']

Instead of the expected

['0.00000000000000000E0', '0.00000000000000000E0', '0.00000000000000000E0'] 
like image 974
Paul Würtz Avatar asked Nov 18 '25 07:11

Paul Würtz


1 Answers

Change the regex to

regex=r"[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?"
                  ^          ^^

The point is to use a non-capturing group instead of the capturing one so that findall did not have to return only the captured text (when there are no capturing groups defined in the pattern, re.findall will return whole matched texts).

Also, use a raw string literal always to define regex pattern to avoid any other misunderstanding.

A Python demo:

import re
regex=r"[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?"
string="0.00000000000000000E0   0.00000000000000000E0   0.00000000000000000E0"
print(re.findall(regex,string))
like image 170
Wiktor Stribiżew Avatar answered Nov 19 '25 21:11

Wiktor Stribiżew