Reputation: 20848
I have the following C# code to replace .
's and +
's with spaces:
string MyString = "this.is.just+an++example--here!are$more characters";
MessageBox.Show(Regex.Replace(MyString, @"[\.\+]", " "));
Sometimes this might result in excess whitespace (as in, between an
and example
).
How can I also add the following RegEx to my existing RegEx, so there is only a single RegEx call?
Regex.Replace(MyString, @"[ ]{2,}", " ");
This will remove excess whitespace. All other characters should remain untouched.
Any alternate solutions are also welcome!
Upvotes: 0
Views: 87
Reputation: 2852
You should try this. You don't need to escape the .
if it is inside a character class
[.\+]+|[ ]{2,}
Upvotes: 2
Reputation: 16291
Maybe you should match multiple characters in the first place. Change the regexp to "[\.\+]+"
to match one or more .
or +
signs. For example, "a...++b"
would result in "a b"
MessageBox.Show(Regex.Replace(MyString, @"[\.\+]+", " "));
Upvotes: 2