Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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
Php 匹配Regexp中的所有内容,直到它达到某个字符串 $str='href='http://grego.com“href=”httpxoobar“href=”xxx“href=”ZZZZZ“; 预匹配所有('/http:\/\/(?)?_Php_Regex - Fatal编程技术网

Php 匹配Regexp中的所有内容,直到它达到某个字符串 $str='href='http://grego.com“href=”httpxoobar“href=”xxx“href=”ZZZZZ“; 预匹配所有('/http:\/\/(?)?

Php 匹配Regexp中的所有内容,直到它达到某个字符串 $str='href='http://grego.com“href=”httpxoobar“href=”xxx“href=”ZZZZZ“; 预匹配所有('/http:\/\/(?)?,php,regex,Php,Regex,我想创建4个匹配项,我想匹配所有在它后面没有“http://”的href=“that”,然后获取href=“(this)”(我正在使用\s\s,因为它可能包含新行)中的内容,当它找到一个引号(“)时,它停止并继续获取下一个引号(在本例中是在同一行中) 在这个例子中,它应该带来所有4个结果 我该怎么做?谢谢。你把事情搞混了 您已经将http://作为匹配的一部分,尽管您正在编写不想匹配的代码 你使用的是一个消极的回顾,而积极的回顾是有意义的 您没有使用/s选项来允许点匹配换行符 你正在使用一个贪

我想创建4个匹配项,我想匹配所有在它后面没有“http://”的href=“that”,然后获取href=“(this)”(我正在使用\s\s,因为它可能包含新行)中的内容,当它找到一个引号(“)时,它停止并继续获取下一个引号(在本例中是在同一行中)

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


我该怎么做?谢谢。

你把事情搞混了

  • 您已经将
    http://
    作为匹配的一部分,尽管您正在编写不想匹配的代码
  • 你使用的是一个消极的回顾,而积极的回顾是有意义的
  • 您没有使用
    /s
    选项来允许点匹配换行符
  • 你正在使用一个贪婪的量词,它会匹配太多,并且
  • 您正在使用正则表达式来匹配HTML
也就是说,你可能会侥幸逃脱:

$str = 'href="http://grego.com" href="httpxoobar" href="xxx" href="ZZZZZ"';

preg_match_all('/http:\/\/(?<!href=")([\s\S]+)"/', $str,$m);


print_r($m);

(?这就是你的意思吗???(我特意在第二个href中添加了一个新行来测试该案例)嗨,Mellamokb,谢谢你的回答,你所做的仍然匹配href=”,我需要不匹配它们,只匹配href=“azauhz”href=“无论什么”,在引号后没有http://的HREF。太好了!非常感谢,它可以工作。但是我有一个问题,在开始时使用分隔符%和/有什么区别?我只看到了/作为分隔符。再次感谢^^您可以选择几乎任何非字母数字字符作为分隔符(或者像
)这样的成对的,如果正则表达式包含斜杠,这是有意义的。否则,您必须转义斜杠(
http:\/\/
)。
(?<=href=")(?!http://)[^"]+
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];