arrow_back A regular expression does not see the "-" symbol. How to solve it?

1 vote
There are url patterns, but regular expressions do not see the "-" sign.

Here is the code leading to the regular:
$to_match = preg_replace('/{([a-z]+):([^\}]+)}/', '(?P<\1>\2)', $to_match);
$to_match = '#^'.$to_match.'$#';

Problem: A template is given, for example item/{name:\w+} . If you go to item/item_name , then everything is fine. But if you go to item/item-name It does not find any more matches.

1 comment

so read what \w means in the regulars, there is no hyphen.

2 Answers

0 votes
0 votes
Try to screen out the special characters, including the hyphen, before you pass the pattern. That is, instead of this item/item-name pass it on like this: item\/item\-name