Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert spaces between characters using Regex?

Tags:

c#

regex

Trying to learn a little more about using Regex (Regular expressions). Using Microsoft's version of Regex in C# (VS 2010), how could I take a simple string like:

"Hello"

and change it to

"H e l l o"

This could be a string of any letter or symbol, capitals, lowercase, etc., and there are no other letters or symbols following or leading this word. (The string consists of only the one word).

(I have read the other posts, but I can't seem to grasp Regex. Please be kind :) ).

Thanks for any help with this. (an explanation would be most useful).

like image 677
Alan Wayne Avatar asked Dec 23 '14 04:12

Alan Wayne


People also ask

How do you add a space in RegEx?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

What is the use of \n in RegEx?

"\n" matches a newline character.

What does '$' mean in RegEx?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What is difference [] and () in RegEx?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .


1 Answers

You could do this through regex only, no need for inbuilt c# functions. Use the below regexes and then replace the matched boundaries with space.

(?<=.)(?!$)

DEMO

string result = Regex.Replace(yourString, @"(?<=.)(?!$)", " ");

Explanation:

  • (?<=.) Positive lookbehind asserts that the match must be preceded by a character.
  • (?!$) Negative lookahead which asserts that the match won't be followed by an end of the line anchor. So the boundaries next to all the characters would be matched but not the one which was next to the last character.

OR

You could also use word boundaries.

(?<!^)(\B|b)(?!$)

DEMO

string result = Regex.Replace(yourString, @"(?<!^)(\B|b)(?!$)", " ");

Explanation:

  • (?<!^) Negative lookbehind which asserts that the match won't be at the start.
  • (\B|\b) Matches the boundary which exists between two word characters and two non-word characters (\B) or match the boundary which exists between a word character and a non-word character (\b).
  • (?!$) Negative lookahead asserts that the match won't be followed by an end of the line anchor.
like image 99
Avinash Raj Avatar answered Sep 28 '22 10:09

Avinash Raj