Removing commas from numbers with .NET regex


Removing commas from numbers with .NET regex



So I'm processing a report that (brilliantly, really) spits out number values with commas in them, in a .csv output. Super useful.



So, I'm using (C#)regex lookahead positive and lookbehind positive expressions to remove commas that have digits on both sides.



If I use only the lookahead, it seems to work. However when I add the lookbehind as well, the expression breaks down and removes nothing. Both ends of the comma can have arbitrary numbers of digits around them, so I just want to remove the comma if the pattern has one or more digits around it.



Here's the expression that works with the lookahead only:


str = Regex.Replace(str, @"[,](?=(d+)),"");



Here's the expression that doesn't work as I intend it:


str = Regex.Replace(str, @"[,](?=(d+)?<=(d+))", "");



What's wrong with my regex! If I had to guess, there's something I'm misunderstanding about how lookbehind works. Any ideas?





I have no idea, but I would suggest Regex.Replace(str, @"(d),(d)", "$1$2")
– Slai
11 hours ago


Regex.Replace(str, @"(d),(d)", "$1$2")





How would you know if 1,2,3,4 is 1, 2, 3, 4 or 1.2, 3.4 or 1, 2.3, 4 or some other combination?
– Andreas
10 hours ago


1,2,3,4


1, 2, 3, 4


1.2, 3.4


1, 2.3, 4





This whole idea might be useless if you don't have another separator than a comma or at least the values in some kind of quotes (see Andreas' commen)t. An if you have that, then the simple regex of Slai is sufficient.
– Javatasse
10 hours ago





I agree with Andreas. That ambiguity is going to mean you will need to solve the original problem. The thing which creates that CSV file isn't actually creating a valid CSV file, because it should have quoted the values containing commas. If you can't fix that, maybe you can trick it by running it in a locale which uses dot as the decimal point.
– Richardissimo
10 hours ago





You may try Regex.Replace(s, @"(?<=d),(?=d)", string.Empty), but it seems the original approach might need re-considering.
– Wiktor Stribiżew
10 hours ago


Regex.Replace(s, @"(?<=d),(?=d)", string.Empty)




2 Answers
2



You can always check a website that evaluates regex expressions.
I think this code might be able to help you:


str = Regex.Replace(str, @"[,](?=(d+))(?<=(d))", "");



You may use any of the solutions below:


var s = "abc,def,2,100,xyz!,:))))";
Console.WriteLine(Regex.Replace(s, @"(d),(d)", "$1$2")); // Does not handle 1,2,3,4 cases
Console.WriteLine(Regex.Replace(s, @"(d),(?=d)", "$1")); // Handles consecutive matches with capturing group+backreference/lookahead
Console.WriteLine(Regex.Replace(s, @"(?<=d),(?=d)", "")); // Handles consecutive matches with lookbehind/lookahead, the most efficient way
Console.WriteLine(Regex.Replace(s, @",(?<=d,)(?=d)", "")); // Also handles all cases



See the C# demo.



Explanations:


(d),(d)


,


$1$2


(d),(?=d)


,


(?=d)


,


$1


(?<=d),(?=d)


(?<=d)


,(?<=d,)(?=d)



RegexHero.net test:



enter image description here



Bonus:



You may just match a pattern like yours with d,d and pass the match to the MatchEvaluator method where you may manipulate the match further:


d,d


MatchEvaluator


Console.WriteLine(Regex.Replace(s, @"d,d", m => m.Value.Replace(",",string.Empty))); // Callback method






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?