How to exclude some strings in C# Regex? -
this current code:
var formula = "scan: \"sample.test\" or 'batch.id' , if (results.tune)))"; if (formula.indexof("field(", stringcomparison.ordinalignorecase) == -1) { formula = regex.replace(formula, "[a-za-z]\\w+\\.[a-za-z_]\\w*", "field(\"$0\")"); }
the output looks like:
"scan: \"field(\"sample.test\")\" or 'field(\"batch.id\")' , if (field(\"results.tune\"))))"
however, i'd skip first 2 matches. so, if term quoted, not replace it. expected output should like:
"scan: \"sample.test\" or 'batch.id' , if (field(\"results.tune\"))))"
i managed expected result using 2 passes:
var formula = "scan: \"sample.test\" or 'batch.id' , if (results.tune)))"; if (formula.indexof("field(", stringcomparison.ordinalignorecase) == -1) { formula = regex.replace(formula, "[a-za-z]\\w+\\.[a-za-z_]\\w*", "field(\"$0\")"); formula = regex.replace(formula, "([\"|'])field\\(\"([a-za-z]\\w+\\.[a-za-z_]\\w*)\"\\)\\1", "$1$2$1"); }
however, believe should possible make in 1 pass, possibly using regex exclude feature, couldn't figure out how.
you can perhaps use negative lookarounds , word boundaries help:
(?<![""'])\b[a-za-z]\w+\.[a-za-z_]\w*\b(?![""'])
also, can use @
avoid having double escape lot of things:
formula = regex.replace(formula, @"(?<![""'])\b[a-za-z]\w+\.[a-za-z_]\w*\b(?![""'])", "field(\"$0\")");
(?<![""'])
negative lookbehind, makes sure character before first match isn't quote.
(?![""'])
negative lookahead, little similar lookbehind, except checks following character.
Comments
Post a Comment