What is the regular expression for removing ONE space? e.g:
H e l l o W o r l d ----> Hello World
(Notice that there's still one space in between Hello World. It has two space in between to begin with)
FYI, I'm working with C# regex: Previously I did something like this, but it doesn't work properly for the above case:
Regex pattern = new Regex(@"[ ]{2,}");
pattern.Replace(content, @" ")
To remove one space from all groups of one or spaces, use
pattern = Regex.Replace(content, " ( *)", "$1");
To change n spaces to floor(n/2) spaces, use
pattern = Regex.Replace(content, " ( ?)", "$1");
I tried to add examples but stackoverflow consolidates whitespace even in inline code spans it seems.
Explanation, as requested: The first finds a space followed by zero or more spaces and replaces it with the zero or more spaces, reducing the length by 1. The second finds each group of one or two spaces and replaces it by zero or one spaces, changing 1 to 0 in one replacement, 2 to 1 in one replacement, 3 to 2 in two replacements, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With