Reputation: 3
I have big .txt file which I am reading using StreamReader
(C# .NET).
My requirement is to replace any of the special characters not in the list to replace with space in entire string (multiple occurrences).
List of special characters allowed is & / - ' . ) (
.
So far, I have tried this but it is not working the way I want it:
aLine = Regex.Replace(aLine, "[^0-9A-Za-z().&'/-]+$", " ");
Upvotes: 0
Views: 2445
Reputation: 70732
Your current regex is doing this:
String input = "123abc[]]]]]]456:$def";
String aLine = Regex.Replace(input, "[^0-9A-Za-z().&'/-]+$", " ");
//=> "123abc[]]]]]]456:$def"
^^^^^^^^^^^^^^^^^^^^^ original string (did not replace)
Remove the end of string $
anchor:
String input = "123abc[]]]]]]456:$def";
String aLine = Regex.Replace(input, "[^0-9A-Za-z().&'/-]+", " ");
//=> "123abc 456 def"
If you want to leave a space for every instance, remove the quantifier:
String input = "123abc[]]]]]]456:$def";
String aLine = Regex.Replace(input, "[^0-9A-Za-z().&'/-]", " ");
//=> "123abc 456 def"
Upvotes: 2