使用python re.findall 分割线

Use python re.findall to split the line

我正在尝试使用 re.findall 拆分一个字符串:

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)

但我得到了:

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

缺少第二个字符串“2”。我想要的是:

['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)

您占了 . 的缺失,但后面的任何内容都没有。所以用\d*,我们修复它。

我会在这里使用 re.findall

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)

这会打印:

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

下面是对正则表达式模式的解释:

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

这里的想法是,每个数字的左边界要么是 \b 字边界,要么数字以负号开头。

这对我有用,你可以查看并告诉我你是否需要其他东西

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 "*"

输出

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

您可以简单地组合两个正则表达式模式来过滤出所需的数字,如下所示:

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']

第一个模式-?\d+.?\d+

-?\d+.? - 获取任何整数,无论​​是否存在负分数。例如,它匹配 -0.

\d+ - 获取小数点后的数字

第二种模式

\d+ - 获取任何整数。例如2315