How to use ^/$ if it's already used as a delimiter for regex in PHP?

后端 未结 2 1799
猫巷女王i
猫巷女王i 2021-01-22 02:37
$regex = \'$....$\'

$regex = \'^...^\'

In the above two cases,how to use ^/$ to match the beginning/end of a string?

相关标签:
2条回答
  • 2021-01-22 03:06

    You can use any non-alphanumeric, non-backslash, non-whitespace character as delimiter. But you shouldn’t use characters that have a special meaning in regular expressions.

    So you could use the ~ if you don’t want to use /:

    '~^/$~'
    
    0 讨论(0)
  • 2021-01-22 03:06

    just do escaping ?

    $str='^...^';
    preg_match("/^\^.*\^$/",$str,$matches);
    print_r($matches);
    
    0 讨论(0)
提交回复
热议问题