php – Regex for 1 or 2 characters after slash
Using /[1-3][0-9]
matches at least 2 digits, from 11 till 39
Using [1-31]{1,2}
is the same as [1-3]{1,2}
and matches 1 or 2 times a range 1-3
If you only want the numbers using php:
/K(?:[1-9]|[12][0-9]|3[01])b
If you want to make the pattern more specific you can see this page to match an ip number.
The pattern matches:
/
Match a forward slash (Using a different delimiter like~
you don’t have to escape the/
)K
Forget what is matched so far(?:[1-9]|[12][0-9]|3[01])
Match 1-9 or 10-29 or 30 or 31b
A word boundary to prevent a partial word match
See a regex demo and a PHP demo.
$str="123.23.23.4/17,";
$str .= '123.23.23.4/23,';
$str .= '123.23.23.4/8,';
$str .= '123.23.23.4/18';
preg_match_all("~/K(?:[1-9]|[12][0-9]|3[01])b~", $str, $matches);
print_r($matches[0]);
Output
Array
(
[0] => 17
[1] => 23
[2] => 8
[3] => 18
)
Read more here: Source link