1

我想在<body>标记示例之后找到所有以两个斜杠开头的字符:-

http://www
// this is first comment
<body>
<div>
// this is comment
<p>//this is another comment.

所以我想同时匹配两者:

// this is comment.
//this is another comment.

但不是:

//www
// this is first comment

这只是一个例子,它可能还包含数字和括号。语言 php 只想要正则表达式

4

2 回答 2

4

您可以使用此 PHP 代码:

$html = <<< EOF
http://www
// this is first comment
<body>
<div>
// this is comment
<p>//this is another comment.
EOF;

解决方案 1:负前瞻

if (preg_match_all('~//(?!.*?<body>)[^\n]*~is', $html, $arr))
   print_r($arr);

解决方案 2:没有前瞻

$html = preg_replace('#^.*?<body>#is', '', $html);
if (preg_match_all('~//[^\n]*~', $html, $arr))
   print_r($arr);

输出:

Array
(
    [0] => Array
        (
            [0] => // this is comment
            [1] => //this is another comment.
        )

)
于 2012-08-18T17:38:20.827 回答
1

你可以用这个模式做到这一点:

(?<!http:)\/\/(\s?[\w\.])+

例子

于 2012-08-18T17:23:02.433 回答