473,388 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 18772
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: fossil_blue | last post by:
Dear Gurus, I am trying to find out how to write an effective regular expression in python for the following scenario: "any number of leading spaces at the beginning of a line" "follow by a...
2
by: Oriana | last post by:
Hi! I'm trying to 'clean up' this source file using regular expressions in Python. My problem is, that when I try to delete extra lines, my code fails. Here's an example.... /** * *...
4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
8
by: Natalia DeBow | last post by:
Hi, I am stuck trying to come up with a regular expression for the following pattern: A string that contains "/*" but that does not contain */ within it. Basically I am searching for C-style...
4
by: Xavier | last post by:
hello, in a string which i read from a textfile there are some lines, which must be eliminated. The content of the stringvariable is for example : myString=" --comment 1 Update ........
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.