Skip to content
Advertisement

regex to detect trailing comma and/or space separated numbers [closed]

I need a regex to detect and match some values like that:

Lorem ipsum 1,2,3
Lorem ipsum 1,2,3,
Lorem ipsum 1, 2, 3
Lorem ipsum 1 2 3
Lorem 12 ipsum 1 2 3

Lorem ipsum 12,13,14

Lorem ipsum 1,12,15
Lorem ipsum 1/12/15

...and so on...

but values like following, are not allowed:

Lorem ipsum
Lorem ipsum 1
Lorem ipsum 2
Lorem ipsum 12
Lorem 12 ipsum 12
Lorem ipsum 1,2,3 and more

One more requirement I have, that i need to use it with PHP preg_match and extract all mathces into variable

Advertisement

Answer

Regex can get tough, but, if i’ve understood your use case correctly, this should work:

(?=d+[ ,/])([d,/s]{2,})$

As shown in this example

Breakdown:

  • (?=d+[ ,/]) positive lookahead asserting that what comes after must include at least one number and one of a space, comma, or slash
  • ([d,/s]{2,}) capture group asserting a match of at least 2 of number space, comma, or slash
  • $ asserts the end of the string

NOTE:

if you want to allow multiple spaces, tabs, new lines etc, change (?=d+[ ,/]) to (?=d+[s,/])


As for getting and working with the matches, something like this should work as shown in this example:

<?php
$re = '/(?=d+[ ,/])([d,/s]{2,})$/';
$str = 'Lorem ipsum 1,2,3
Lorem ipsum 1,2,3,
Lorem ipsum 1, 2, 3
Lorem ipsum 1 2 3
Lorem 12 ipsum 1 2 3

Lorem ipsum 12,13,14

Lorem ipsum 1,12,15
Lorem ipsum 1/12/15

Lorem ipsum
Lorem ipsum 1
Lorem ipsum 2
Lorem ipsum 12
Lorem 12 ipsum 12
Lorem ipsum 1,2,3 and more';

// just getting an arry so we can loop over them easily
$values = array_filter(array_map('trim', explode("n", $str)));

foreach($values as $value) {
    
    echo "'$value'";
    
    preg_match($re, trim($value), $matches);
    
    if (isset($matches[0])) {
        echo " matches...n";
        $output = preg_split( "/[ ,/]/", $matches[0]);
        $output = array_map('trim', $output);
        $output = array_filter($output);
        var_dump($output);
    } else {
        echo " doesn't match...";
    }
    echo "n";
    echo "n";
}

Which produces this output:

‘Lorem ipsum 1,2,3’ matches…

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}


'Lorem ipsum 1,2,3,' matches...
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}


'Lorem ipsum 1, 2, 3' matches...
array(3) {
  [0]=>
  string(1) "1"
  [2]=>
  string(1) "2"
  [4]=>
  string(1) "3"
}


'Lorem ipsum 1 2 3' matches...
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}


'Lorem 12 ipsum 1 2 3' matches...
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}


'Lorem ipsum 12,13,14' matches...
array(3) {
  [0]=>
  string(2) "12"
  [1]=>
  string(2) "13"
  [2]=>
  string(2) "14"
}


'Lorem ipsum 1,12,15' matches...
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(2) "12"
  [2]=>
  string(2) "15"
}


'Lorem ipsum 1/12/15' matches...
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(2) "12"
  [2]=>
  string(2) "15"
}


'Lorem ipsum' doesn't match...

'Lorem ipsum 1' doesn't match...

'Lorem ipsum 2' doesn't match...

'Lorem ipsum 12' doesn't match...

'Lorem 12 ipsum 12' doesn't match...

'Lorem ipsum 1,2,3 and more' doesn't match...
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement