$str = 'href="http://grego.com" href="httpxoobar" href="xxx" href="ZZZZZ"';
preg_match_all('/http:'/'/(?<!href=")(['s'S]+)"/', $str,$m);
print_r($m);
我正在尝试这个代码。
我想创建4个匹配,我想匹配所有的href=",没有"http://"之后,然后得到什么是在href="(这个)"(我使用's' s,因为它可能包含新行),当它发现一个引号(")时,它停止并继续获取下一个(在这种情况下是在同一行),
在这个例子中,它应该会显示所有4个结果。
我该怎么做?谢谢。
你把事情弄混了。
- 你已经让
http://
成为匹配的一部分,尽管你写你不想要匹配它, - 你在用消极的眼光看后面,而积极的眼光是有意义的,
- 你没有使用
/s
选项来允许点匹配换行符, - 你使用的贪心量词会匹配太多,而
- 你正在使用正则表达式来匹配HTML
也就是说,你可以这样做:
(?<=href=")(?!http://)[^"]+
。在PHP中:
preg_match_all(
'%(?<=href=") # Assert position right after href="
(?!http://) # Assert that http:// is not right ahead
[^"]+ # Match one or more characters until the next "
%x',
$subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];