繁体   English   中英

正则表达式查找所有单词以两个斜杠开头

[英]Regex find all words start with two slashes

我想在<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只想要正则表达式

您可以使用以下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.
        )

)

您可以使用以下模式进行操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM