Reputation: 5876
I am attempting to regex replace all the commas in-between quotations in a string. I can't seem to get it to match them all in a non-greedy fashion so it won't just regex the entire line. The text and regex I'm trying are below. With the regex I'm trying, it is picking up the commas in-between each element. EX "remove,comma","nocomma" yields "remove,comma" and a "," match. Also, It is picking up matches like "nocomma"," from "nocomma","nocomma".
I was working on the matching first, then narrowing it down to replacing the commas.
Using .NET 4.0
TEXT Before Regex
"remove,comma","remove,comma","nocomma","remove,comma","remove,all,commas,here,too","also,remove,commas"
TEXT I Want After Before Regex
"removecomma","removecomma","nocomma","removecomma","removeallcommasheretoo","alsoremovecommas"
REGEX
\".*?(,).*?\"
Upvotes: 1
Views: 119
Reputation: 32787
Regex.Replace(input,@",(?!([^""]*""[^""]*"")*[^""]*$)","");
The above regex would replace ,
only if their are odd number of "
ahead..This is because ,
within ""
would have odd number of "
ahead.
[^"]*
would match 0 to many characters which are not "
$
depicts the end of string
(?!)
is a negative lookahead
Upvotes: 1
Reputation: 172378
How about this:-
Regex.Replace(input,@",(?![^""]*""(?:[^""]*""[^""]*"")*[^""]*$)",String.Empty);
Or try this:-
string pattern = @"""[^""]*""";
data = Regex.Replace(data, pattern, m => m.Value.Replace(",", ""),
RegexOptions.IgnorePatternWhitespace);
Upvotes: 1