Victorgalaxy
Victorgalaxy

Reputation: 397

Regular expression for removing one space

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, @" ")

Upvotes: 3

Views: 233

Answers (7)

jaytea
jaytea

Reputation: 1949

By no means a general solution given the nature of your problem, but in this particular case it looks as though you could get away with removing spaces that touch a word character on either side: on the left, for example:

Regex.Replace(content, " \b", "");

Upvotes: 0

Jonas Elfström
Jonas Elfström

Reputation: 31428

Matches a whitespace followed by a non-whitespace and removes those whitespaces.

Regex.Replace(input, @"(\s)(\S)", @"$2");

It kind of looks like it's a string with an added space after each character. If it's so then you could get the original string by retrieving only the even indexed characters in the string.

var input = "H e l l o   W o r l d ";
var res = String.Join("", input.Where((c, i) => (i % 2) == 0));

Upvotes: 0

sepp2k
sepp2k

Reputation: 370142

If I understand you want to remove exactly one space for each occurrence of one or more consecutive spaces.

For that you need to create a regex which matches each such occurrence putting all but one of the spaces into a capturing group and then replace each occurrence with capturing group. So if there are 2 spaces next to each other, they're found as one match and the second space goes in the capturing group. So after the replacement two spaces have been reduced to one space.

Regex pattern = new Regex(@" ( *)");
String newString = pattern.Replace("H e l l o  W o r l d", "$1");
// newString == "Hello World"

Upvotes: 1

Charles
Charles

Reputation: 11479

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.

Upvotes: 5

Kevin McKelvin
Kevin McKelvin

Reputation: 3547

Another way would be to use a match evaluator to use a two character match:

string s = Regex.Replace("H e l l o  W o r l d", @"\s\S", x => x.Value[1].ToString());

Upvotes: 0

viggity
viggity

Reputation: 15237

Try using a negative look ahead.

Regex pattern = new Regex(@"\s(?!\s)");
Console.WriteLine(pattern.Replace(content, ""))

Upvotes: 2

jaywayco
jaywayco

Reputation: 6286

The expression "[\s]{1}" should do it

Upvotes: -1

Related Questions