你可以使用这样的正则表达式:
([-.]?\d+(?:\.\d+)?)
比赛信息:
MATCH 1
1. [1-6] `0.286`
MATCH 2
1. [6-12] `-3.099`
MATCH 3
1. [12-17] `-0.44`
MATCH 4
1. [18-24] `-2.901`
MATCH 5
1. [25-31] `-0.436`
MATCH 6
1. [34-37] `123`
MATCH 7
1. [38-43] `0.123`
MATCH 8
1. [44-47] `.34`
更新
正如Jawee在他的评论中指出的那样.34.34存在问题,所以你可以使用他的正则表达式解决这个问题.感谢Jawee指出这一点.
(-?(?:\d+)?\.?\d+)
要了解这个正则表达式背后发生的事情你可以查看Debuggex
图片:
发动机说明:
1st Capturing group (-?(?:\d+)?\.?\d+)
-? -> matches the character - literally zero and one time
(?:\d+)? -> \d+ match a digit [0-9] one and unlimited times (using non capturing group)
\.? matches the character . literally zero and one time
\d+ match a digit [0-9] one and unlimited times