简体   繁体   English

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

[英]Regex find all words start with two slashes

I want to find all the characters that start with two slashes after <body> tag example:- 我想在<body>标签示例之后找到所有以两个斜杠开头的字符:-

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

so I want to match both: 所以我想同时匹配:

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

but not: 但不是:

//www
// this is first comment

This is just example it might also contains digits and brackets. 这仅是示例,它可能还包含数字和括号。 language php just want regex 语言PHP只想要正则表达式

You can use this PHP code: 您可以使用以下PHP代码:

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

Solution 1: With negative lookahead 解决方案1:前瞻性为负

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

Solution 2: Without lookaheads 解决方案2:无需先行

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

OUTPUT: 输出:

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

)

You can do it with this pattern: 您可以使用以下模式进行操作:

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

example

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

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