SEARCH YOUR SOLUTION HERE  

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:  418
View Complete with Replies




Related Messages:

FAQ 6.10 What is "/o" really for?   (87 Views)
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.
FAQ 6.10 What is "/o" really for?   (61 Views)
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.
FAQ 6.10 How do I use a regular expression to strip C style comments from a file?   (98 Views)
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.
FAQ 6.10 How do I use a regular expression to strip C style comments from a file?   (96 Views)
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
FAQ 6.10 How do I use a regular expression to strip C style comments from a file?   (111 Views)
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.
Can't use an undefined value as an ARRAY reference at search.cgi line 139.   (133 Views)
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 ...
FAQ 8.41 How do I use an SQL database?   (132 Views)
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 ...
Pause Perl Program?   (83 Views)
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 ...
the use of removeChild() in Tree::Simple   (125 Views)
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 ...
FAQ 5.34 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work?   (62 Views)
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 ...
Re: Continuous communication between user, CGI script and server   (86 Views)
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....
Listing all modules used by program?   (89 Views)
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 ...
tk user widgets   (92 Views)
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 ...
Using SendKeys- Simulate or Double mouse Click   (75 Views)
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. ...
Re: Excuse my obviousness   (100 Views)
--- 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. ...
Why does "use base" grow the call stack exponentially?   (67 Views)
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 ...
Correct use of Unicode in RegExp   (83 Views)
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 ...
Confused on using basename...   (108 Views)
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 ...
Adding nt user to global group   (109 Views)
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 ...
How to propagate -w and use strict to evaled code.   (80 Views)
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 $@; ...