使用 Preg_match_all 和“\”字符
Usin Preg_match_all with "\" character
我正在尝试使用 preg_match_all
从字符串中获取表达式
但它不能正常工作我试试这个:
$string='this is a test hello 34';
preg_match_all('[(\)(0-9)], $string, $output);
它只显示 "hello" 如果我退出 $string 的“\”我可以看到 "hello 1234" 但是对于“\”它不起作用。
如果你使用这样的东西:
/^(hello \[0-9]*)$/g
这会给你一个完整的。请参阅 (
和 [
变体。
说明
我想我可能有适合你的解决方案
$string='this is a test hello 34';
preg_match("/hello \\[0-9]*/", $string, $output);
var_dump($output);
这里的东西是 4 个反斜杠。我不是 100% 确定这一点,但我认为这是因为在 php 中你需要转义反斜杠所以 \
产生 \
在正则表达式中,您还需要两个反斜杠(请参阅 praveen kumars 的回答以获得良好的正则表达式解释)。因此,当您使用 4 个反斜杠时,php 将其更改为 2,这就是此正则表达式
所需要的
编辑:
事实证明我的想法有些道理。查看 this 文章以获得更多解释
我正在尝试使用 preg_match_all
但它不能正常工作我试试这个:
$string='this is a test hello 34';
preg_match_all('[(\)(0-9)], $string, $output);
它只显示 "hello" 如果我退出 $string 的“\”我可以看到 "hello 1234" 但是对于“\”它不起作用。
如果你使用这样的东西:
/^(hello \[0-9]*)$/g
这会给你一个完整的。请参阅 (
和 [
变体。
说明
我想我可能有适合你的解决方案
$string='this is a test hello 34';
preg_match("/hello \\[0-9]*/", $string, $output);
var_dump($output);
这里的东西是 4 个反斜杠。我不是 100% 确定这一点,但我认为这是因为在 php 中你需要转义反斜杠所以 \
产生 \
在正则表达式中,您还需要两个反斜杠(请参阅 praveen kumars 的回答以获得良好的正则表达式解释)。因此,当您使用 4 个反斜杠时,php 将其更改为 2,这就是此正则表达式
编辑: 事实证明我的想法有些道理。查看 this 文章以获得更多解释