RegEx Challenge: Capture all the numbers in a specific row

Viewed 102

Assume we have this text:

...
settingsA=9, 4.2 
settingsB=3, 1.5, 9, 2, 4, 6
settingsC=8, 3, 2.5, 1
...

The question is how can I capture all the numbers that are in specific row using a single step?

Single step means:

  • single regex pattern.
  • single operation (no loops or splits, etc.)
  • all matches are captured in one array.

Let's say I want to capture all the numbers that are present in row which starts with settingsB=. The final result should look like this:

3
1.5
9
2
4
6

My failed attempts:

<?php
    $subject =
        "settingsA=9, 4.2
         settingsB=3, 1.5, 9, 2, 4, 6
         settingsC=8, 3, 2.5, 1";

    $pattern = '([\d\.]+)(, )?' // FAILED!
    $pattern = '(?:settingsB=)(?:([\d\.]+)(?:, )?)' // FAILED!
    $pattern = '(?:settingsB=)(?:([\d\.]+)(?:, )?)+' // FAILED!
    $pattern = '(?<=^settingsB=|, )([\d+\.]+)' // FAILED!

    preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
    if ($matches) {
        print_r($matches);
    }
?>

UPDATE 1: @Saleem's example uses multiple steps instead of a single step, unfortunately. I'm not saying that his example is bad (it actually works), but I want to know if there is another way to do it and how. Any ideas?

UPDATE 2: @bobble bubble provided a perfect solution for this challenge.

2 Answers
Related