tbonejenkins
tbonejenkins

Reputation: 379

Regex - add space between replaced characters

I have a regex @"\bAND\b|\bOR\b|""|\(|\)" that strips out certain words or characters from a string. The problem I have is that if the following text is used:

Over ear headphones"OR"on ear headphones

When I use regex.replace, the regex successfully strings out 'OR' but the two queries will be mashed together

Over ear headphoneson ear headphones

This only happens when there isn't a space between the word between the word I want to strip and the other query terms. Is there an elegant way to add spaces between keywords I want to strip, if none exist.

Upvotes: 2

Views: 644

Answers (3)

user557597
user557597

Reputation:

This should preserve the existing format (as well as can be expected).

To use non-linebreak whitespace, substitute the \s with [^\s\r\n]
or similar.


Just optionally consume a single whitespace on either side of the expression, then replace with a whitespace of your choice.

This is done on items surrounded with text.

A<sp><item>B     => A<sp>B  
A<sp><item>B<sp> => A<sp>B  
A<item><sp>B     => A<sp>B  
A<item>B         => A<sp>B  

For items at the beginning or end of the string
replace with the empty string.

This is the regex @"(^)?\s?(?:\b(?:AND|OR)\b|[()""])+\s?($)?"

Expanded

 ( ^ )?                        # (1)
 \s? 
 (?:
      \b 
      (?: AND | OR )
      \b 
   |  [()"] 
 )+
 \s? 
 ( $ )?                        # (2)

C#

string sTrg = @"""Over ear headphones""OR""on ear headphones""";
Console.WriteLine("{0}", sTrg);

Regex rX = new Regex(@"(^)?\s?(?:\b(?:AND|OR)\b|[()""])+\s?($)?");
Console.WriteLine("{0}", rX.Replace(sTrg,
    delegate (Match m) {
       if (m.Groups[1].Success || m.Groups[2].Success)
           return "";
       return " ";
   }));

Output

"Over ear headphones"OR"on ear headphones"
Over ear headphones on ear headphones

Upvotes: 0

ΩmegaMan
ΩmegaMan

Reputation: 31596

Add a space regardless in the replacement text, then create a subsequent regex replace to strip out 2 or more spaces when this condition is not hit.

That way you are guaranteed that one space will be between each of the words.

This is a basic example (without your quoted "OR") for brevity but the concept is the same:

Regex.Replace(Regex.Replace(txt, "OR", " "), @"\s\s", " ")

Result

Over ear headphones on ear headphones

Upvotes: 0

Sus
Sus

Reputation: 79

I know that it may sound silly, but have you tried replacing this with a single space instead of just removing it?

Upvotes: 1

Related Questions