Connecting Tech Pros Worldwide Help | Site Map

Regular expression to get multi line comment

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:14 PM
Rob
Guest
 
Posts: n/a
Default Regular expression to get multi line comment

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









  #2  
Old July 17th, 2005, 12:14 PM
Daniel Tryba
Guest
 
Posts: n/a
Default 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

  #3  
Old July 17th, 2005, 12:14 PM
Rob
Guest
 
Posts: n/a
Default 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



  #4  
Old July 17th, 2005, 12:14 PM
Daniel Tryba
Guest
 
Posts: n/a
Default 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
  #5  
Old July 17th, 2005, 12:14 PM
Daedalus
Guest
 
Posts: n/a
Default 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]


  #6  
Old July 17th, 2005, 12:14 PM
Rob
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.