Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Regex: Identifying comments

by pvaldes (Chaplain)
on Aug 30, 2012 at 10:06 UTC ( #990708=note: print w/replies, xml ) Need Help??

in reply to Regex: Identifying comments

The comments in a sql file will be probably either at the start of the line with or without white spaces "^\s*--" or after a semicolon "\;\s*--". If I'm not wrong, this is not a valid sql sentence in most databases, thus you shouldn't have it:

select '--foo' from mytable --bar

(And you shouldn't have tables with name fields beginning by "--")

Something like this should be enough to strip all comments from your file:

open my ORIGINALFILE, '<', 'file.sql' or die... etc open my CLEANEDFILE, '>', outfile... etc, as usual while (<ORIGINALFILE>){ next if /^\s*--.*$/ ; s/\;\s*--.*$/;/ ; print CLEANEDFILE $_ ; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990708]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-05-24 18:56 GMT
Find Nodes?
    Voting Booth?