Reputation: 15
I've been grappling with some negative lookahead and lookbehind patterns to no avail. I need a regex that will match everything in a string before two forward slashes, unless said characters are in quotes.
For example, in the string "//this is a string//" //A comment about a string about strings
,
the substring "//this is a string//"
ought to be matched and the rest ignored.
As you can see, the point is to exclude any single-line comments (C++/Java style).
Thanks in advanced.
Upvotes: 1
Views: 1485
Reputation: 17128
A python/regex based comment remover I wrote a while back, if it's helpful:
def remcomment(line):
for match in re.finditer('"[^"]*"|(//)', line):
if match.group(1):
return line[:match.start()].rstrip()
return line
Upvotes: 0
Reputation: 12930
How about
\/\/[^\"']*$
It will match //
if it is not followed by either a "
or a '
. It's not exactly what you requested, but closely meets your requirements. It will only choke on comments that contain "
or '
, like
// I like "bread".
Maybe better than no solution.
Upvotes: 1