dev*_*raj 1 regex pcre preg-replace
我有这个简单的虚拟文本
<base href="http://wjbty.lc/"/?
<a href="common/home" />
<a href="common/home" />
<a href="/common/home" />
<a href="http://common/home" />
<a href="https://common/home" />
<a href="common/home" />
Run Code Online (Sandbox Code Playgroud)
我的正则表达式模式是 (?:(href="))(?!\/)(?!https:\/\/)(?!http:\/\/)(.*)"
它的工作正常并匹配所有相关链接,但它始终包含href="在结果匹配中。
以及如何href="从结果中排除。任何人都可以测试它然后回答请,因为有两个答案,但没有一个有效。

在您的表达式中,您有一组额外的括号
(?:(href="))
Run Code Online (Sandbox Code Playgroud)
它应该是
(?:href=")
Run Code Online (Sandbox Code Playgroud)
编辑:我想你想要这个
/(?:href=")(?!\/)(?!https:\/\/)(?!http:\/\/)(.*)"/
Run Code Online (Sandbox Code Playgroud)
所以它也不会捕获关闭引号。还要记住 preg_match_all 返回一个数组或数组,第一个数组集是总捕获。第二个数组集是组捕获(你想要的)
额外参数
flags 参数可以是 PREG_PATTERN_ORDER 或 PREG_SET_ORDER
PREG_PATTERN_ORDER 表示 array[0] 将是所有捕获信息,而 array[1] 将是您在括号中捕获的信息。
PREG_SET_ORDER 意味着每个匹配都会有一个数组元素,match[0] 是总信息,match[1] 是捕获组。