Regex: how to "retreat"

I am having trouble preparing a regex that produces this result:

Mike 1 , Misha 1.2 , Miguel 1,2,3,4,5,6,7,18 and Michea 2,3

How to take one step back in regex and discard the last match? That is, I need a comma so that the space does not match. This is what I came up with ...

\d+(,|\r)

Mike 1, Misha 1.2, Miguel 1,2,3,4,5,6,7,18, and Michil 2,3

+3
source share
2 answers

, , . , . :

\d+(?:,\d+)*

- .

, PHP, :

<?php
$input = "Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Micheal2,3";
$matches = array();
preg_match_all('/\d+(?:,\d+)*/', $input, $matches);
print_r($matches[0]);
?>

:

Array
(
    [0] => 1
    [1] => 1,2
    [2] => 1,2,3,4,5,6,7,18
    [3] => 2,3
)
+2

, \d+,(?!\s) , . ?! , , ?! .

>>> re.findall(r'\d+,(?!\s)', 'Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Michea2,3')
['1,', '1,', '2,', '3,', '4,', '5,', '6,', '7,', '2,']

, , , , \d+(?:,\d+)*.

>>> re.findall(r'\d+(?:,\d+)*', 'Mike1, misha1,2, miguel1,2,3,4,5,6,7,18, and Michea2,3')
['1', '1,2', '1,2,3,4,5,6,7,18', '2,3']
0

Source: https://habr.com/ru/post/1781144/


All Articles