preg_match_all regex值在两个标记之间


preg_match_all regex value in between two tags

我正试图构建一个preg_match_all,这样我就可以使用该数组,但问题是我无法完善我的正则表达式。

一个示例输入是:

#|First Name|#
This text shouldn't be caught
#|Second Value|#

第一个名称和第二个值都应该在数组中。

以下是我尝试过的:

preg_match_all('/'#'|(.*?)'|'#']/',$html, $out);

preg_match_all ("/^#'|*'|#*$/i", $html, $out);

第一个差不多是对的,它只在接近末尾的地方包含'],它会破坏您想要的内容。使用

/'#'|(.*?)'|'#/

确实给了我正确的结果,在$out[1]中匹配。

试试这个正则表达式:

/'#'|(.*?)'|'#/

尝试使用:

preg_match_all('/#'|(.*?)'|#/', $html, $out);

不应该需要转义#。要指定#之间没有#,您也可以使用

preg_match_all('/#'|([^#]*)'|#/', $html, $out);

有几件事,您需要添加"."角色在你的酒吧之间。此字符表示匹配任何字符,如果没有它,则表示匹配零个或多个"|"字符。您还需要为多行匹配添加m修饰符。

/^#'|.*'|#$/im