What would be the best regular expression for the following entry "200 # 5; 300 # 10"?
What would be the best regular expression for the following entry "200 # 5; 300 # 10"?
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"
}
}