Regex: match, but not if in a comment

You could simply anchor the regex to the start of the line:

(?m)^(\d+),\s(\S+),\s(\S+),\s(\S+)

It seems to me you could just anchor the expression at the beginning of the line (to get all the data):

^(\d+),\s(data),\s(data),\s(data)\s*(?://|$)

Or maybe you can use a proper CSV parser which can handle comments.


The easy way out is to replace \s*//.* with the empty string before you begin.

This will remove all the (single-line) comments from your input and you can go on with a simple expression to match what actually you want.

The alternative would be to use look-ahead instead of look-behind:

^(?!//)(\b\d+\b),\s(data),\s(data),\s(data)

In your case it would even work to just anchor the regex because it is clear that the first thing on a line must be a digit:

^(\b\d+\b),\s(data),\s(data),\s(data)

Some regex engines (the one in .NET, for example), support variable-length look-behinds, your's does not seem to be capable of this, this is why (?<!//\s*) fails for you.

Tags:

Regex

Comments