简体   繁体   中英

Use python re.findall to split the line

I am trying to use re.findall to split one string:

string = '1.1 2 -4259.8774  0.000000  0.707664  0.002210 -0.004314-0.004912-0.000823'

I tried with:

match = re.findall(r'-?\d+\.?\d+m?', string)

but I got:

['1.1', '-4259.8774', '0.000000', '0.707664', '0.002210', '-0.004314', '-0.004912',
 '-0.000823']

The second string '2' is missing. What I want is:

['1.1', '2',  '-4259.8774', '0.000000', '0.707664', '0.002210', '-0.004314', '-0.004912',
 '-0.000823']

I would use re.findall here:

string = '1.1 2 -4259.8774  0.000000  0.707664  0.002210 -0.004314-0.004912-0.000823'
nums = re.findall(r'(?:\b|-)\d+(?:\.\d+)?', string)
print(nums)

This prints:

['1.1', '2', '-4259.8774', '0.000000', '0.707664', '0.002210', '-0.004314', '-0.004912',
 '-0.000823']

Here is an explanation of the regex pattern:

(?:\b|-)       match either a word boundary OR a minus sign, which is followed by
\d+(?:\.\d+)?  a whole number with optional decimal component

The idea here is that the left boundary of each number is either a \b word boundary, or the number starts with a minus sign.

Updated

Just do:

match = re.findall( r'-?\d+\.?\d*m?'  , string)

You accounted for missing the . , but not for anything following it. So with \d* , we fix it.

This worked for me you can check and let me know if something else that you need

import re
string='1.1 2 -4259.8774  0.000000  0.707664  0.002210 -0.004314-0.004912-0.000823'
match = re.findall( r'-?\d*\.?\d+m?'  , string)#After first \d i replace "+" with "*"

Output

['1.1',
 '2',
 '-4259.8774',
 '0.000000',
 '0.707664',
 '0.002210',
 '-0.004314',
 '-0.004912',
 '-0.000823']

You can simply combine two regex patterns to filter out the desired numbers as below:

import re

>>> string='1.1 2 -4259.8774  0.000000  0.707664  0.002210 -0.004314-0.004912-0.000823'
>>> re.findall('-?\d+.?\d+|\d+', string)
>>> ['1.1', '2', '-4259.8774', '0.000000', '0.707664', '0.002210', '-0.004314', '-0.004912', '-0.000823']

In the first pattern -?\d+.?\d+ the

-?\d+.? - Fetches any integer whether or not a negative fraction exists. For instance, it matches -0.

\d+ - Fetches the digit after the decimal

In the second pattern

\d+ - Fetches any whole numbers. For example, 2 , 3 , 15 etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM