Regex: match, but not if in a comment -
i have file of data fields, may contain comments, below:
id, data, data, data 101 a, b, c 102 d, e, f 103 g, h, // has 101 a, b, c 104 j, k, l //105 m, n, o // 106 p, q, r
as can see in first comment above, there direct references matching pattern. now, want capture 103 , it's 3 data fields, don't want capture what's in comments.
i've tried negative lookbehind exclude 105 , 106, can't come regex capture both.
(?<!//)(\b\d+\b),\s(data),\s(data),\s(data)
this capture exclude capture of 105, specify
(?<!//\s*) or (?<!//.*)
as attempt exclude comment whitespace or characters invalidates entire regex.
i have feeling need crafty use of anchor, or need wrap want in capture group , make reference (like $1
) in lookbehind.
if case of "regular expressions don't support recursion" because it's regular language (a la automata theory), please point out.
is possible exclude comments in 103, , lines 105 , 106, using regular expression? if so, how?
the easy way out replace \s*//.*
empty string before begin.
this remove (single-line) comments input , can go on simple expression match want.
the alternative use look-ahead instead of look-behind:
^(?!//)(\b\d+\b),\s(data),\s(data),\s(data)
in case work anchor regex because clear first thing on line must digit:
^(\b\d+\b),\s(data),\s(data),\s(data)
some regex engines (the 1 in .net, example), support variable-length look-behinds, your's not seem capable of this, why (?<!//\s*)
fails you.
Comments
Post a Comment