Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Lookbehind断言-匹配链接锚文本_Regex_Boost Regex - Fatal编程技术网

Regex Lookbehind断言-匹配链接锚文本

Regex Lookbehind断言-匹配链接锚文本,regex,boost-regex,Regex,Boost Regex,我有像这样的链接 <a href="#" class="social google">Google</a> <a href="#" class="social yahoo">Yahoo</a> <a href="#" class="social facebook">Facebook</a> 但它并没有像预期的那样工作 谁能给我正确的语法吗?试试这个 "~<a(>| .*?>)(.*?)</a&g

我有像这样的链接

<a href="#" class="social google">Google</a>
<a href="#" class="social yahoo">Yahoo</a>
<a href="#" class="social facebook">Facebook</a>
但它并没有像预期的那样工作

谁能给我正确的语法吗?

试试这个

  "~<a(>| .*?>)(.*?)</a>~si"
“~|.*?>)(.*?~si”

“/|.*?>)(.*?)
php示例

  $notecomments ='<a id="234" class="asf">fdgsd</a> <a>fdgsd</a>';

  $output=preg_replace_callback(array("~<a(>| .*?>)(.*?)</a>~si"),function($matches){
       print_r($matches[2]);
       return '';
   },' '.$notecomments.' ');
$notecomments='fdgsd-fdgsd';
$output=preg_replace_回调(数组(“~|.*?>)(.*)~si”),函数($matches){
打印($matches[2]);
返回“”;
},“.$notecomments.”);
这将为您提供所有锚文本

此返回仅限class=“社会”

“#(.*)#”
样品

  $notecomments ='<a id="234" class="fas social ads">fdgsd</a> <a>fdgsd</a>';

  $output=preg_replace_callback(array("#<a .*?class=\".*?social.*?\".*?>(.*?)</a>#"),function($matches){

     print_r($matches);
 return '';},' '.$notecomments.' ');
$notecomments='fdgsd-fdgsd';
$output=preg#u replace#回调(数组(“#(.*?)#”)函数($matches){
打印(匹配项);
返回“”;}、.$notecomments.');

您可能得到了正确的结果,但因为您有其他匹配组(?…),所以您的匹配也包含了不需要的数据


您可以尝试使用不匹配的组(?:…),并将您希望在匹配中显示的内容放在组本身中(+?)

而不是使用“向后看”和“向前看”来排除您不想要的部分,我建议使用捕获组来仅获取您想要的部分:

<a href="#" class="social .+?">(.+?)</a>

从概念上讲,环视用于重叠匹配。这里似乎不需要它们的功能

(当然,这是适用的)

更新:这不仅仅是最佳实践的问题。使用look-behind的正则表达式实际上会产生错误的结果,因为它允许look-behind部分与其他匹配重叠。考虑这个输入:

<a href="#" class="social google">Google</a>

...

<a class="bad">foo</a>

...
福

你的正则表达式将不仅匹配“谷歌”;它还将匹配“foo”,因为假定只匹配类字符串的一部分的
可以一直扩展到文本中的另一个链接。

尝试以下正则表达式:

\<a .*?\>(.*?)\<\/a\>
\(*?)\
编辑1-此正则表达式与css类为“social”的锚匹配:


\n您只想要class=“social”的a元素吗?@Giri:就像我在前面的问题中所说的那样。如果内容和类是任意的,则不可能仅匹配标记内的文本。根本没有支持。@nhahdh是的,我理解。但我正在寻找替代方案。我认为这个解决办法会奏效@Giri:这与其他用户在上一个问题(编辑后)中提供的解决方案相同。@nhahdh,我认为这与您提到的解决方案相同<代码>通常捕获组就足以满足大多数替换方案
。因为我对regex不熟悉,所以我第一次看不懂它。嗨,当我使用你的代码时,它仍然用标记选择整个文本。你能告诉我怎么了吗?@Giri,你需要使用正确的Boost函数来获取捕获的子组,而不是整个子组。我不是一个Boost用户,但看起来他们在这里展示了如何做到这一点:您好,是的,我只想要具有class=“social”Hi的a元素,但我不需要所有锚文本。我只需要具有class=“social”@Giri Yes的a元素,我理解可以看到更改。实际上,后面的可变长度查找可能在匹配之间重叠,导致正则表达式错误地匹配页面中的其他链接和其他元素。
<a href="#" class="social .+?">(.+?)</a>
<a href="#" class="social google">Google</a>

...

<a class="bad">foo</a>
\<a .*?\>(.*?)\<\/a\>
\<a .*?class=".*?\bsocial\b.*?\>(.*?)\<\/a\>