FAQ 6.10 How do I use a regular expression to strip C style comments from a file?


This is an excerpt from the latest version perlfaq6.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest ...
Posted On: Tuesday 27th of November 2012 11:50:12 PM Total Views:  535
View Complete with Replies

RELATED TOPICS OF Perl PROGRAMMING LANGUAGE




FAQ 6.10 What is "/o" really for?

This is an excerpt from the latest version perlfaq6.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 6.10: What is "/o" really for (contributed by brian d foy) The "/o" option for regular expressions (documented in perlop and perlreref) tells Perl to compile the regular expression only once. This is only useful when the pattern contains a variable. Perls 5.6 and later handle this automatically if the pattern does not change. Since the match operator "m//", the substitution operator "s///", and the regular expression quoting operator "qr//" are double-quotish constructs, you can interpolate variables into the pattern. See the answer to "How can I quote a variable to use in a regex" for more details. This example takes a regular expression from the argument list and prints the lines of input that match it: my $pattern = shift @ARGV; while( ) { print if m/$pattern/; } Versions of Perl prior to 5.6 would recompile the regular expression for each iteration, even if $pattern had not changed. The "/o" would prevent this by telling Perl to compile the pattern the first time, then reuse that for subsequent iterations: my $pattern = shift @ARGV; while( ) { print if m/$pattern/o; # useful for Perl < 5.6 } In versions 5.6 and later, Perl won't recompile the regular expression if the variable hasn't changed, so you probably don't need the "/o" option. It doesn't hurt, but it doesn't help either. If you want any version of Perl to compile the regular expression only once even if the variable changes (thus, only using its initial value), you still need the "/o". You can watch Perl's regular expression engine at work to verify for yourself if Perl is recompiling a regular expression. The "use re 'debug'" pragma (comes with Perl 5.005 and later) shows the details. With Perls before 5.6, you should see "re" reporting that its compiling the regular expression on each iteration. With Perl 5.6 or later, you should only see "re" report that for the first iteration. use re 'debug'; $regex = 'Perl'; foreach ( qw(Perl Java Ruby Python) ) { print STDERR "-" x 73, "\n"; print STDERR "Trying $_...\n"; print STDERR "\t$_ is good!\n" if m/$regex/; } -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

110

Posted on:

Wednesday 3rd October 2012
View Replies!

FAQ 6.10 What is "/o" really for?

This is an excerpt from the latest version perlfaq6.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 6.10: What is "/o" really for (contributed by brian d foy) The "/o" option for regular expressions (documented in perlop and perlreref) tells Perl to compile the regular expression only once. This is only useful when the pattern contains a variable. Perls 5.6 and later handle this automatically if the pattern does not change. Since the match operator "m//", the substitution operator "s///", and the regular expression quoting operator "qr//" are double-quotish constructs, you can interpolate variables into the pattern. See the answer to "How can I quote a variable to use in a regex" for more details. This example takes a regular expression from the argument list and prints the lines of input that match it: my $pattern = shift @ARGV; while( ) { print if m/$pattern/; } Versions of Perl prior to 5.6 would recompile the regular expression for each iteration, even if $pattern had not changed. The "/o" would prevent this by telling Perl to compile the pattern the first time, then reuse that for subsequent iterations: my $pattern = shift @ARGV; while( ) { print if m/$pattern/o; # useful for Perl < 5.6 } In versions 5.6 and later, Perl won't recompile the regular expression if the variable hasn't changed, so you probably don't need the "/o" option. It doesn't hurt, but it doesn't help either. If you want any version of Perl to compile the regular expression only once even if the variable changes (thus, only using its initial value), you still need the "/o". You can watch Perl's regular expression engine at work to verify for yourself if Perl is recompiling a regular expression. The "use re 'debug'" pragma (comes with Perl 5.005 and later) shows the details. With Perls before 5.6, you should see "re" reporting that its compiling the regular expression on each iteration. With Perl 5.6 or later, you should only see "re" report that for the first iteration. use re 'debug'; $regex = 'Perl'; foreach ( qw(Perl Java Ruby Python) ) { print STDERR "-" x 73, "\n"; print STDERR "Trying $_...\n"; print STDERR "\t$_ is good!\n" if m/$regex/; } -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

82

Posted on:

Tuesday 16th October 2012
View Replies!

FAQ 6.10 How do I use a regular expression to strip C style comments from a file?

This is an excerpt from the latest version perlfaq6.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 6.10: How do I use a regular expression to strip C style comments from a file While this actually can be done, it's much harder than you'd think. For example, this one-liner perl -0777 -pe 's{/\*.*\*/}{}gs' foo.c will work in many but not all cases. You see, it's too simple-minded for certain kinds of C programs, in particular, those with what appear to be comments in quoted strings. For that, you'd need something like this, created by Jeffrey Friedl and later modified by Fred Curtis. $/ = undef; $_ = ; s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 $2 : ""#gse; print; This could, of course, be more legibly written with the "/x" modifier, adding whitespace and comments. Here it is expanded, courtesy of Fred Curtis. s{ /\* ## Start of /* ... */ comment [^*]*\*+ ## Non-* followed by 1-or-more *'s ( [^/*][^*]*\*+ )* ## 0-or-more things which don't start with / ## but do end with '*' / ## End of /* ... */ comment | ## OR various things which aren't comments: ( " ## Start of " ... " string ( \\. ## Escaped char | ## OR [^"\\] ## Non "\ )* " ## End of " ... " string | ## OR ' ## Start of ' ... ' string ( \\. ## Escaped char | ## OR [^'\\] ## Non '\ )* ' ## End of ' ... ' string | ## OR . ## Anything other char [^/"'\\]* ## Chars which doesn't start a comment, string or escape ) }{defined $2 $2 : ""}gxse; A slight modification also removes C++ comments, as long as they are not spread over multiple lines using a continuation character): s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 $2 : ""#gse; -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

168

Posted on:

Saturday 20th October 2012
View Replies!

FAQ 6.10 How do I use a regular expression to strip C style comments from a file?

This is an excerpt from the latest version perlfaq6.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 6.10: How do I use a regular expression to strip C style comments from a file While this actually can be done, it's much harder than you'd think. For example, this one-liner perl -0777 -pe 's{/\*.*\*/}{}gs' foo.c will work in many but not all cases. You see, it's too simple-minded for certain kinds of C programs, in particular, those with what appear to be comments in quoted strings. For that, you'd need something like this, created by Jeffrey Friedl and later modified by Fred Curtis. $/ = undef; $_ = ; s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 $2 : ""#gse; print; This could, of course, be more legibly written with the "/x" modifier, adding whitespace and comments. Here it is expanded, courtesy of Fred Curtis. s{ /\* ## Start of /* ... */ comment [^*]*\*+ ## Non-* followed by 1-or-more *'s ( [^/*][^*]*\*+ )* ## 0-or-more things which don't start with / ## but do end with '*' / ## End of /* ... */ comment | ## OR various things which aren't comments: ( " ## Start of " ... " string ( \\. ## Escaped char | ## OR [^"\\] ## Non "\ )* " ## End of " ... " string | ## OR ' ## Start of ' ... ' string ( \\. ## Escaped char | ## OR [^'\\] ## Non '\ )* ' ## End of ' ... ' string | ## OR . ## Anything other char [^/"'\\]* ## Chars which doesn't start a comment, string or escape ) }{defined $2 $2 : ""}gxse; A slight modification also removes C++ comments, as long as they are not spread over multiple lines using a continuation character): s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 $2 : ""#gse; -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod. -- Posted via a free Usenet account from http://www.teranews.com
VIEWS ON THIS POST

142

Posted on:

Monday 22nd October 2012
View Replies!

FAQ 6.10 How do I use a regular expression to strip C style comments from a file?

This is an excerpt from the latest version perlfaq6.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 6.10: How do I use a regular expression to strip C style comments from a file While this actually can be done, it's much harder than you'd think. For example, this one-liner perl -0777 -pe 's{/\*.*\*/}{}gs' foo.c will work in many but not all cases. You see, it's too simple-minded for certain kinds of C programs, in particular, those with what appear to be comments in quoted strings. For that, you'd need something like this, created by Jeffrey Friedl and later modified by Fred Curtis. $/ = undef; $_ = ; s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 $2 : ""#gse; print; This could, of course, be more legibly written with the "/x" modifier, adding whitespace and comments. Here it is expanded, courtesy of Fred Curtis. s{ /\* ## Start of /* ... */ comment [^*]*\*+ ## Non-* followed by 1-or-more *'s ( [^/*][^*]*\*+ )* ## 0-or-more things which don't start with / ## but do end with '*' / ## End of /* ... */ comment | ## OR various things which aren't comments: ( " ## Start of " ... " string ( \\. ## Escaped char | ## OR [^"\\] ## Non "\ )* " ## End of " ... " string | ## OR ' ## Start of ' ... ' string ( \\. ## Escaped char | ## OR [^'\\] ## Non '\ )* ' ## End of ' ... ' string | ## OR . ## Anything other char [^/"'\\]* ## Chars which doesn't start a comment, string or escape ) }{defined $2 $2 : ""}gxse; A slight modification also removes C++ comments, as long as they are not spread over multiple lines using a continuation character): s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 $2 : ""#gse; -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

159

Posted on:

Sunday 4th November 2012
View Replies!

Can't use an undefined value as an ARRAY reference at search.cgi line 139.

Hi all, I have what seems to me to be a strange problem.... I have the script below: if ($type eq 'alpha') { $query = sprintf ( "SELECT name, address, city, phone FROM valley where name like '$search%' ORDER ...
VIEWS ON THIS POST

180

Posted on:

Monday 26th November 2012
View Replies!

FAQ 8.41 How do I use an SQL database?

This is an excerpt from the latest version perlfaq8.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest ...
VIEWS ON THIS POST

189

Posted on:

Monday 26th November 2012
View Replies!

Pause Perl Program?

Newbieish question: I have a program that I need to have pause for a second (or less), then continue. I RTFM'ed and I found the Sleep function and Win32::Sleep. It seems like the Win32::Sleep module is a better choice as ...
VIEWS ON THIS POST

114

Posted on:

Monday 26th November 2012
View Replies!

the use of removeChild() in Tree::Simple

I am currently using Tree::Simple module. I originally have a tree x. My task is to insert a node G into x, so that the B node under A go under G. My solution is: I create G first, then ...
VIEWS ON THIS POST

188

Posted on:

Monday 26th November 2012
View Replies!

FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest ...
VIEWS ON THIS POST

86

Posted on:

Monday 26th November 2012
View Replies!

Re: Continuous communication between user, CGI script and server

Sherm Pendley wrote: > Have a look at AJAX. Also take a look at the CGI::Ajax module. It will do a lot of the client side scripting for you....
VIEWS ON THIS POST

119

Posted on:

Monday 26th November 2012
View Replies!

Listing all modules used by program?

Greetings all, I'm trying to figure out how to automatically list all the modules used by a particular program. The closest I've come is creating a CPAN autobundle but this includes everything on the system, which isn't what I'm hoping ...
VIEWS ON THIS POST

119

Posted on:

Monday 26th November 2012
View Replies!

tk user widgets

Hi i read here few times there is no one place to check for all user widgets. probably most of them are located on cpan but no all. what do you think of setting up a wiki for such purpose ...
VIEWS ON THIS POST

120

Posted on:

Monday 26th November 2012
View Replies!

Using SendKeys- Simulate or Double mouse Click

I'm trying to simulate using SendKeys to highlight text on an entry box. I use SendKeys("{HOME"); to mark the beginning of the text and then I tried SendKeys("{SHIFT}{END}"); to highlight the text and only the END action seems to happen. ...
VIEWS ON THIS POST

109

Posted on:

Monday 26th November 2012
View Replies!

Re: Excuse my obviousness

--- gerardo castro wrote: > I dont know, but...is there any perl/tk gui > builder is there any > one compact is there anyone for windows and... > any one of them is > free Give ZooZ a try. ...
VIEWS ON THIS POST

129

Posted on:

Monday 26th November 2012
View Replies!

Why does "use base" grow the call stack exponentially?

I was trying to find out a way for a parent class to register its subclasses, and made experiments with require. While the result was succesful, in that I did find a way to register the subclasses, it also gave ...
VIEWS ON THIS POST

91

Posted on:

Monday 26th November 2012
View Replies!

Correct use of Unicode in RegExp

I am having great difficulty using Unicode characters in a Regular Expression, I am trying to match extended Unicode characters. I am wishing to split a large Dumpfile (containing only JPEGS) I have used a hex editor to manually extract ...
VIEWS ON THIS POST

118

Posted on:

Monday 26th November 2012
View Replies!

Confused on using basename...

I have an online ftp type website for a customer that I wrote in a cgi perl script. If the person is trying to upload a file that already exists on the server I am printing a message telling them ...
VIEWS ON THIS POST

139

Posted on:

Monday 26th November 2012
View Replies!

Adding nt user to global group

I can't understand why I cant add users to a group using this script. If I change the Netadmin line to read GroupIsMember instead of GroupAddUsers, I can check if the user belongs to a group. the error message I ...
VIEWS ON THIS POST

147

Posted on:

Monday 26th November 2012
View Replies!

How to propagate -w and use strict to evaled code.

I have this template code (wptk.template): use Tk; use vars qw/ $mw /; use strict; $mw = MainWindow->new; print "wptk.template ARGV=@ARGV!\n"; $^W = 1; my $stat = do "$ARGV[0]"; if ( not defined $stat ) { print $@ if $@; ...
VIEWS ON THIS POST

115

Posted on:

Monday 26th November 2012
View Replies!