匹配Regexp中的所有内容,直到达到某个字符串PHP

时间:2011-09-22 16:07:00

标签: php regex

$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 =”(this)中的内容“(我'使用\ s \ S因为它可能包含新行)并且当它找到引号(“)时它停止并继续获取下一个(在这种情况下是在同一行),

在这个例子中它应该带来所有4个结果。

我该怎么做?感谢。

1 个答案:

答案 0 :(得分:1)

你的事情有些混乱。

  • 你已经将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];