Reputation: 25
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']
Upvotes: 1
Views: 876
Reputation: 2210
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.
Upvotes: 0
Reputation: 248
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']
Upvotes: 0
Reputation: 4799
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.
Upvotes: 0
Reputation: 521279
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.
Upvotes: 2