Search code examples
regexregex-lookarounds

How to find more than one match of numbers utilizing regex


I am attempting to pick apart data from the following string utlizing a regex expression:

Ethane, C2 11.7310 3.1530 13.9982 HV, Dry @ Base P,T 1432.00

The ultimate goal is to be able to pull out the middle three data points as individual values 11.7310, 3.153, 13.9982

The code expression I am working with at the moment is as follows:

(?<=C2 )(\d*\.?\d+)

This yields a full match of 11.7310 and a Group 1 match of 11.7310, but I can't figure out how to match the other two data points.

I am using PCRE (PHP) to create my expression.


Solution

  • You may use

    (?:\G(?!^)|\bC2)\s+\K\d*\.?\d+
    

    See the regex demo.

    Details

    • (?:\G(?!^)|\bC2) - either the end of the previous successful match or C2 whole word
    • \s+ - 1+ whitespaces
    • \K - match reset operator discarding all the text matched so far in the match memory buffer
    • \d* - 0+ digits
    • \.? - an optional dot
    • \d+ - 1+ digits.