Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Regex: Identifying comments

by choroba (Abbot)
on Aug 29, 2012 at 14:33 UTC ( #990483=note: print w/ replies, xml ) Need Help??


in reply to Regex: Identifying comments

This should work for simple cases (no newlines inside single quotes, no single quotes in comments):

#!/usr/bin/perl use warnings; use strict; while (<DATA>) { my $last = (split /'/)[-1]; print $1 if $last =~ /(--.*)/; } __DATA__ select 'text' from foo --This is a comment select '--Not a valid comment' from foo --But this is select q from z -- as is this select '--This is not a valid comment' from foo select '--Not this' + '--either' from foo
Updated.
To handle single quotes in comments, you might need to change the script to the following:
#!/usr/bin/perl use warnings; use strict; while (<DATA>) { my @items = split /'/; until (not @items or $items[0] =~ s/.*?--/--/) { shift @items for 1, 2; # Remove the quoted part, too. } print join "'", @items; } __DATA__ select 'text' from foo --This is a comment select '--Not a valid comment' from foo --But this is select q from z -- as is this select '--This is not a valid comment' from foo select '--Not this' + '--either' from foo select 'qaws' + make from "a" -- comment with 'a' quote select 'a' from 'b' with 'c' -- comment with 'a --' comment
To get the code instead of the comments, just invert the logic:
while (<DATA>) { chomp; my @code; my @items = split /'/; until (not @items or $items[0] =~ s/--.*//) { @items and push @code, shift @items for 1, 2; } print join("'", @code), @items ? (@code ? "'" : q() ) . "$items[0] +" : q(), "\n"; }
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Regex: Identifying comments
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990483]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-10-24 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls