Php 用于拆分哈希标记的正则表达式,但忽略
我有一个字符串,我想在其中匹配特定“上下文”后的hashtag,例如Php 用于拆分哈希标记的正则表达式,但忽略,php,regex,Php,Regex,我有一个字符串,我想在其中匹配特定“上下文”后的hashtag,例如|product 在| product之后,我想匹配后面的hashtag 这是我的完整字符串| product#houtprint#laserprint | materialal#hout 这是我的正则表达式模式,直到现在\\124; product(?#[^\\\124;]+)。 我现在在#houtprint#laserprint上找到了匹配项,但我想分别在#houtprint和#laserprint上找到匹配项 这也是我的P
|product
在| product
之后,我想匹配后面的hashtag
这是我的完整字符串| product#houtprint#laserprint | materialal#hout
这是我的正则表达式模式,直到现在\\124; product(?#[^\\\124;]+)
。
我现在在#houtprint#laserprint
上找到了匹配项,但我想分别在#houtprint
和#laserprint
上找到匹配项
这也是我的PHP部分:
preg\u match\u all(“~\\\””$context.(?#[^\\\]+)~,$tags\u string,$matches)代码>
如何确保将产品作为单独的组获取?您需要设置基于\G
的边界,以便preg\u match\u all
可以匹配连续的哈希标记(在您指定的子字符串之后紧随其后):
(?:\|product|\G(?!\A))(?<product>#[^|#]+)
$re = '/(?:\|product|\G(?!\A))(?<product>#[^|#]+)/';
$str = '|product#houtprint#laserprint|materiaal#hout';
preg_match_all($re, $str, $matches);
print_r($matches["product"]);
// => Array ( [0] => #houtprint [1] => #laserprint )