Python Regex匹配行中的第二个或第三个单词

时间:2021-08-31 15:23:27

I'm trying to separate lines into 3 sections using regex, with a typical line fitting into this kind of pattern: -30.345 150.930 112.356

我正在尝试使用regex将行划分为3个部分,其中典型的行符合这种模式:-30.345 150.930 112.356

I'm extracting the first section of data fine using lat = float(re.match('[^\s]+', line).group(0)), but have been unable to correctly target the 2nd and 3rd numbers.

我使用lat = float(re)提取数据的第一部分。匹配(“[^ \ s]+”,线).group(0)),但一直未能正确目标第二和第三个数字。

I've tried/am trying long = float(re.match('.*?\s(\S+)\s.*?', line).group(0)) but this is returning the entire string up until the 2nd whitespace. How can I target just the 2nd number and 3rd number in these strings?

我试着/我尝试长=浮动(re.match('。* ? \ s(\ s +)\ s . * ?但这是在返回整个字符串,直到第二个空格。我怎么能只针对这些字符串中的第二个和第三个数字呢?

1 个解决方案

#1


2  

If you cannot do split then you can just match the numbers with optional - or + at the start:

如果你不能进行分割,那么你可以在开始时将数字与可选的-或+进行匹配:

>>> s = '-30.345 foo 150.930 abc 112.356 another .123'
>>> re.findall(r'([+-]?\d*\.?\d+)', s)
['-30.345', '150.930', '112.356', '.123']

RegEx Demo

RegEx演示

#1


2  

If you cannot do split then you can just match the numbers with optional - or + at the start:

如果你不能进行分割,那么你可以在开始时将数字与可选的-或+进行匹配:

>>> s = '-30.345 foo 150.930 abc 112.356 another .123'
>>> re.findall(r'([+-]?\d*\.?\d+)', s)
['-30.345', '150.930', '112.356', '.123']

RegEx Demo

RegEx演示