Connecting Tech Pros Worldwide Help | Site Map

Regular expression to get multi line comment

Rob
Guest
 
Posts: n/a
#1: Jul 17 '05
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








Daniel Tryba
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Regular expression to get multi line comment


In comp.lang.php Rob <reply@newsgroup.nl> wrote:[color=blue]
> 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 ;-) )[/color]

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

Rob
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Regular expression to get multi line comment



"Daniel Tryba" <partmapsswen@invalid.tryba.nl> schreef in bericht
news:424133d0$0$155$c5fe704e@news6.xs4all.nl...[color=blue]
> In comp.lang.php Rob <reply@newsgroup.nl> wrote:[color=green]
>> 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
>> -) )[/color]
>
> 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
>[/color]

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



Daniel Tryba
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Regular expression to get multi line comment


Rob <reply@newsgroup.nl> wrote:[color=blue]
> I red the docs on the www.php.net[/color]

All of them?
[color=blue]
> $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?[/color]

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
Daedalus
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Regular expression to get multi line comment


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" <reply@newsgroup.nl> wrote in message
news:d1rc5s$ch$1@reader10.wxs.nl...[color=blue]
> 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
>
>
>
>
>
>
>
>[/color]


Rob
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Regular expression to get multi line comment



"Daniel Tryba" <partmapsswen@invalid.tryba.nl> schreef in bericht
news:42414c64$0$149$c5fe704e@news6.xs4all.nl...[color=blue]
> Rob <reply@newsgroup.nl> wrote:[color=green]
>> I red the docs on the www.php.net[/color]
>
> All of them?
>[color=green]
>> $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?[/color]
>
> 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[/color]

Thanks,

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

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

Rob


Closed Thread


Similar PHP bytes