Python Regex match 2nd or 3rd word in line
<p>I'm trying to separate lines into 3 sections using regex, with a typical line fitting into this kind of pattern: <code>-30.345 150.930 112.356</code></p>

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

<p>I've tried/am trying <code>long = float(re.match('.*?\s(\S+)\s.*?', line).group(0))</code> 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?</p>

<p>If you cannot do <code>split</code> then you can just match the numbers with optional <code>-</code> or <code>+</code> at the start:</p>

<pre><code>&gt;&gt;&gt; s = '-30.345 foo 150.930 abc 112.356 another .123'
&gt;&gt;&gt; re.findall(r'([+-]?\d*\.?\d+)', s)
['-30.345', '150.930', '112.356', '.123']
</code></pre>

<p><a href="https://regex101.com/r/DXv0ep/7" rel="nofollow">RegEx Demo</a></p>

python regex