I am trying to extract all strings that look like
12-15
from a parent string. This means all strings that have a dash in between two digits.
Using this answer as a basis, I tried the following:
<?php
$str = "34,56,67-90,45";
preg_match('/^(\d-\d)|(,\d-\d)|(\d-\d,)|(,\d-\d,)$/', $str, $output, PREG_OFFSET_CAPTURE);
echo print_r($output);
?>
This looks for any substring that looks a dash enclosed between digits, whether it has a comma before, after, or both, or none. When I run the PHP code, I get an empty array. On Regex101, when I test the regular expression, strings like
4-5,,,,,
seem to, and I'm not understanding why it's letting me add extra commas.
What's wrong with my regex that I get an empty array?
Answer:
\d
matches a single digit. All the numbers in your sample string have two digits. You should use \d+
to match any number of digits.preg_match('/^(\d+-\d+)|(,\d+-\d+)|(\d+-\d+,)|(,\d+-\d+,)$/', $str, $output, PREG_OFFSET_CAPTURE);
Output:
Array
(
[0] => Array
(
[0] => ,67-90
[1] => 5
)
[1] => Array
(
[0] =>
[1] => -1
)
[2] => Array
(
[0] => ,67-90
[1] => 5
)
)
You can also simplify the regexp:
preg_match('/(?:^|,)\d+-\d+(?:,|$)/', $str, $output, PREG_OFFSET_CAPTURE);
Output:
Array
(
[0] => Array
(
[0] => ,67-90,
[1] => 5
)
)
Answer:
I think you could use a simple regex like this
\d+[-]\d+
That is (match at least 1 digit) (match a literal dash) (match at least 1 digit)
Answer:
The
|
has precedence, meaning your expression is interpreted as "MATCH EITHER ONE OF THE FOLLOWING:- START of text -> 1 digit -> dash -> 1 digit (not matching end of text)
- Comma (may be in the middle of the text, anywhere) -> 1 digit -> dash -> 1 digit
- 1 digit (anywhere) -> dash -> 1 digit -> comma
- comma (anywhere) -> 1 digit -> dash -> 1 digit -> comma -> Followed by the END of text
Also, your are using
\d
which matches 1 digit (only one character). You can use \d{2}
to match 2 digits (00 to 99), or \d+
to match any integer (1, 55, 123456, etc).
In your case, I think you're trying to use this expression:
/(?:^|,)(\d+-\d+)(?:,|$)/
which means: START of text OR comma -> any integer -> dash -> any integer -> comma OR END of text
No comments:
Post a Comment