469,134 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,134 developers. It's quick & easy.

Regular expression to get multi line comment

Rob
Hello all,

If I have the following code fragment:
/*
comment bla bla
*/
....code...

With a regular expression, how do I get/extract the comment inside this
multi line comment block. With and without the comment characters?
And how do I get all the multi line comment from in the entire script?
(yes I have red the documentation, but obviously this wasn't enough ;-) )
Thanks in advance
Rob


Jul 17 '05 #1
5 18249
In comp.lang.php Rob <re***@newsgroup.nl> wrote:
If I have the following code fragment:
/*
comment bla bla
*/
...code...

With a regular expression, how do I get/extract the comment inside this
multi line comment block. With and without the comment characters?
And how do I get all the multi line comment from in the entire script?
(yes I have red the documentation, but obviously this wasn't enough ;-) )


What documentation did you read? AFAIK there is mention of a multiline
flag in the preg section. So all you do need to do is do a nongreedy
multiline match for /\/\*.*?/*///.

FUP comp.lang.php

Jul 17 '05 #2
Rob

"Daniel Tryba" <pa**********@invalid.tryba.nl> schreef in bericht
news:42*********************@news6.xs4all.nl...
In comp.lang.php Rob <re***@newsgroup.nl> wrote:
If I have the following code fragment:
/*
comment bla bla
*/
...code...

With a regular expression, how do I get/extract the comment inside this
multi line comment block. With and without the comment characters?
And how do I get all the multi line comment from in the entire script?
(yes I have red the documentation, but obviously this wasn't enough
-) )


What documentation did you read? AFAIK there is mention of a multiline
flag in the preg section. So all you do need to do is do a nongreedy
multiline match for /\/\*.*?/*///.

FUP comp.lang.php


Thanks for the fast reply,

I red the docs on the www.php.net

I used it in the following code:

$code=<<<end_of_code

/**
* comment 1
*/

codeblock 1

/**
* comment 2
*/

codeblock 2

end_of_code;

$pattern='/\/\*.*?/*///';

if (preg_match($pattern,$code,$array=array())){
print_r($array);
}

I receive the following error:Warning: Unknown modifier '*' in
What do i do wrong?

Thanks Rob

Jul 17 '05 #3
Rob <re***@newsgroup.nl> wrote:
I red the docs on the www.php.net
All of them?
$pattern='/\/\*.*?/*///';

if (preg_match($pattern,$code,$array=array())){
print_r($array);
}

I receive the following error:Warning: Unknown modifier '*' in
What do i do wrong?


You copy and pasted my regexp :) It should be /\/\*.*?\*\//

But still missing is multiline support:
http://nl2.php.net/manual/en/referen....modifiers.php
and
http://nl2.php.net/manual/en/functio...-match-all.php
Jul 17 '05 #4
Try this:

$pattern = '/\/\*(.*)\*\//Us';
preg_match_all($pattern, $string, $result);

$result[0] is an array containing every matches (including /* */) and
$result[1] is another array containing every matches from the first captured
parenthesized subpattern (in this case it only exclude /* */).
Use:
print_r($result);
to view the results

Dae

"Rob" <re***@newsgroup.nl> wrote in message
news:d1*********@reader10.wxs.nl...
Hello all,

If I have the following code fragment:
/*
comment bla bla
*/
...code...

With a regular expression, how do I get/extract the comment inside this
multi line comment block. With and without the comment characters?
And how do I get all the multi line comment from in the entire script?
(yes I have red the documentation, but obviously this wasn't enough ;-) )
Thanks in advance
Rob



Jul 17 '05 #5
Rob

"Daniel Tryba" <pa**********@invalid.tryba.nl> schreef in bericht
news:42*********************@news6.xs4all.nl...
Rob <re***@newsgroup.nl> wrote:
I red the docs on the www.php.net


All of them?
$pattern='/\/\*.*?/*///';

if (preg_match($pattern,$code,$array=array())){
print_r($array);
}

I receive the following error:Warning: Unknown modifier '*' in
What do i do wrong?


You copy and pasted my regexp :) It should be /\/\*.*?\*\//

But still missing is multiline support:
http://nl2.php.net/manual/en/referen....modifiers.php
and
http://nl2.php.net/manual/en/functio...-match-all.php


Thanks,

Adding multiline support(?s) didnot do the trick as explained in the docs.
But the following pattern did:

$pattern='/\*[^*]*\*+([^/*][^*]*\*+)*/';

Rob
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by fossil_blue | last post: by
2 posts views Thread by Oriana | last post: by
8 posts views Thread by Natalia DeBow | last post: by
4 posts views Thread by Xavier | last post: by
3 posts views Thread by Zach | last post: by
25 posts views Thread by Mike | last post: by
3 posts views Thread by Mr.Steskal | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.