13

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).

4 Answers 4

19

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Could you explain the syntax please? Thanks.
@Raj Thanks. All answers were great, but I was looking for a pure Regex statement. Any idea why the downvote on a learning question :( ?
i don't know what's wrong with your question but you must show your attempts.
My attempts resulted in gibberish--not worth the memory space. Thanks!
8
Regex.Replace("Hello", "(.)", "$1 ").TrimEnd();

Explanation

  • The dot character class matches every character of your string "Hello".
  • The paranthesis around the dot character are required so that we could refer to the captured character through the $n notation.
  • Each captured character is replaced by the replacement string. Our replacement string is "$1 " (notice the space at the end). Here $1 represents the first captured group in the input, therefore our replacement string will replace each character by that character plus one space.
  • This technique will add one space after the final character "o" as well, so we call TrimEnd() to remove that.

A demo can be seen here.

For the enthusiast, the same effect can be achieve through LINQ using this one-liner:

String.Join(" ", YourString.AsEnumerable())

or if you don't want to use the extension method:

String.Join(" ", YourString.ToCharArray())

Comments

2

It's very simple. To match any character use . dot and then replace with that character along with one extra space

Here parenthesis (...) are used for grouping that can be accessed by $index

Find what : "(.)"

Replace with "$1 "

DEMO

2 Comments

So how does this look in the final statement? Thanks.
there are lots of modifiers that is used along with regex pattern such as m for multiline and i for case-insensitive and g for global (match all) and others. That are language specific. Read more...
0

You could use single negative lookahead expression:

(?!^|$)

This will select all string.Empty between a string characters except for the string boundaries

string input = "python"
string pattern = @"(?!^|$)";
Regex regex = new Regex(pattern);
var result = regex.Replace(input, " ");
//the result is "p y t h o n"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.