Adjust Python Regex to not include one digit in search results

I am trying to capture / extract numerical values ​​from some strings.

Here is an example line:

s='The shipping company had 93,999,888.5685 gallons of fuel on hand'

I want to pull out the value 93,999,888.5685 I got my regex for this

> mine=re.compile("(\d{1,3}([,\d{3}])*[.\d+]*)")

However, when I do findall, I get the following:

mine.findall(s)

[('93,999,888.5685', '8')]

I tried several different strategies so as not to compare it with 8

But now I understand that I'm not sure I know why it matched 8

Any coverage would be appreciated.

+3
source share
4 answers

, 8 , , 2 . , , ?: : (\d{1,3}(?:[,\d{3}])*[.\d+]*)

([,\d{3}]) .

+4

:

(
\d{1,3}       This will match any group of 1-3 digits (`8`, `12`, `000`, etc)
  (
     [,\d{3}] This will match groups of a "," and 3 digits (`,123`, `,000`, etc)
  )*            **from zero to infinity times**
  [.\d+]*     This matches any number of periods "." and digits from 0 to infinity
)
+1

findall . ( ​​ ) . . , .

>>> mine=re.compile("(\d{1,3}(,\d{3})*(\.?\d+)*)")
>>> s='blah 93,999,888.5685 blah blah blah 988,122.3.'
>>> [m[0] for m in mine.findall(s)]
['93,999,888.5685', '988,122.3']
0

\D? mine=re.compile("\D(\d{1,3}([,\d{3}])[.\d+])\D").

-1

Source: https://habr.com/ru/post/1762523/


All Articles