#delimiters之间的正则表达式匹配

问题描述 投票:-4回答:1

我有一个字符串,完全如图所示,我试图匹配分隔符之间的内容。

# a
This is what should be matched/returned
# \a

# a# \a是分隔符。

PHP /正则表达式不匹配:

$str = <<<EOD

# a
this is what should be returned
# \a

EOD;

preg_match("/#\sa\n(.*)\n#\s\\a/m", $str, $matches);

/*
Array
(
)
*/
print_r($matches);

我究竟做错了什么?

php regex
1个回答
3
投票

有几种方法可以做到:

  1. 用于s.修饰符以匹配换行符
  2. \\\\逃脱\
  3. 可能有一个\r\n而不仅仅是\n所以我用\s

 preg_match("/#\sa\s(.*)\s#\s\\\\a/s", $str, $matches);

为什么你在世界各地使用{1}

© www.soinside.com 2019 - 2024. All rights reserved.