Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
953 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'(?:|-)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:

(?:|-)       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 word boundary, or the number starts with a minus sign.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...