Question

I am looking for a regular expression to remove comment lines from a sql fine. All of the comments start with "COMMENT ON" (obviously) but may tend to have more than one line. I was able to come up with an expression to remove a single line but am struggling with multiple lines. A typical entry I am looking to remove looks like this:

COMMENT ON TABLE account_heading IS $$
This table holds the account headings in the system.  Each account must belong 
to a heading, and a heading can belong to another heading.  In this way it is 
possible to nest accounts for reporting purposes.$$;

So what I need is a regular expression that follows multiple lines up to the point it sees a semicolon.

I came up with this one that will search to the second line and stop at the double dollar ($$). I just started with RegEx yesterday so forgive me if this is absolutely completely wrong (which I am sure it is):

^COMMENT ([^\n\r]+)[\n\r]([\$;\n\r]+)

I am doing this in TextWrangler with the Grep option on a Mac.

Thanks!

Was it helpful?

Solution

Well, I don't know SQL very well, but if you want the content between the '$$', just use:

\${2}[^\${2}]*\${2};

If you want everything after the 'IS' Word:

COMMENT .*? IS ([^;]*)

Matches anything that isn't a semicolon (get the first group, not the match).

OTHER TIPS

Maybe something like COMMENT\sON.*?\$\$; - it should work if you are able to set your editor to match on multiple lines (usually by specifying the s flag).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top