Regular expression to extract numbers from "200 # 5; 300 # 10"

3

What would be the best regular expression for the following entry "200 # 5; 300 # 10"?

    
asked by anonymous 19.10.2014 / 12:42

1 answer

6

You will need two capture groups. Assuming you only have digits, you can use this:

/(\d+)#(\d+)/

The \d represents "digit", and + means "one or more". The parentheses indicate catch groups, and the # indicates exactly the # character.

An example would be: link

$string = "200#5;300#10";
preg_match_all('/(\d+)#(\d+)/', $string, $matches);
var_dump($matches);

// resultado: 
array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "200#5"
    [1]=>
    string(6) "300#10"
  }
  [1]=>
  array(2) {
    [0]=>
    string(3) "200"
    [1]=>
    string(3) "300"
  }
  [2]=>
  array(2) {
    [0]=>
    string(1) "5"
    [1]=>
    string(2) "10"
  }
}
    
19.10.2014 / 12:49