Basic Bridge
Basic Bridge

Reputation: 1911

Regex find all words start with two slashes

I want to find all the characters that start with two slashes after <body> tag example:-

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

Upvotes: 1

Views: 417

Answers (2)

anubhava
anubhava

Reputation: 785406

You can use this PHP code:

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

Solution 1: With negative lookahead

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

Solution 2: Without lookaheads

$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.
        )

)

Upvotes: 4

Adam Wolski
Adam Wolski

Reputation: 5666

You can do it with this pattern:

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

example

Upvotes: 1

Related Questions