473,883 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18804
In comp.lang.php Rob <re***@newsgrou p.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**********@i nvalid.tryba.nl > schreef in bericht
news:42******** *************@n ews6.xs4all.nl. ..
In comp.lang.php Rob <re***@newsgrou p.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($pa ttern,$code,$ar ray=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***@newsgrou p.nl> wrote:
I red the docs on the www.php.net
All of them?
$pattern='/\/\*.*?/*///';

if (preg_match($pa ttern,$code,$ar ray=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***@newsgrou p.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**********@i nvalid.tryba.nl > schreef in bericht
news:42******** *************@n ews6.xs4all.nl. ..
Rob <re***@newsgrou p.nl> wrote:
I red the docs on the www.php.net


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

if (preg_match($pa ttern,$code,$ar ray=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
1531
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 string" "there maybe a string that starts with *" for example:
2
1802
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.... /** * * Project: MyProject *
4
2219
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 = " s^\?AAA\?01^BBB^g; #Comment "
8
2150
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 multiline comments and would much rather use Regex than strings. Here is what I have, but it does not seem to work. Match match = Regex.Match(textLine, @"\s*(/)(?<comment>(?!/).*)$");
4
1784
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 ..... --comment 2 .......
3
2569
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 expression. ^(.+?) uses (?!a spoon)\.$
25
5193
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 (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
3
1846
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 expression that only returns part of a string. For Example I have a multi-line text fragment like below:
0
6205
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 long set of special symbols and impossible to improve such expression. We have to create ’smart’ regular expression. Instead of write one line expression we prepare multi line text from which we shall generate our long expression. Here is a simple...
0
9789
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11129
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10408
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7967
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7119
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.