In this episode, Eric asks us to replace space count with spaces:
--------------------------------------------------------------------------------
Given a string with embedded space counts:
<15sp> Indented by 15 spaces
Replace the <count> SP> with <count> spaces.
So, if you have
<4sp> text
You shoshould end up
Text
----------------------------------------------------------------------------------
And my answer is:
Using system;
Using system. Collections. Generic;
Using system. Text. regularexpressions;
/* RegEx 101 exercise i8-Replace space count with Spaces */
Namespace regexexercisei8
{
Class Program
{
Static void main (string [] ARGs)
{
RegEx = new RegEx (@ "<(\ D +) SP>", regexoptions. Compiled | regexoptions. ignorecase );
Console. writeline ("type in the text you want to process :");
String inputhtml = console. Readline ();
String resulthtml = RegEx. Replace (inputhtml, delegate (match)
{
Return new string ('', int32.parse (match. Groups [1]. Value ));
});
Console. writeline (resulthtml );
}
}
}
I think this exercise is very interesting and helpful, imagine that you are currently working on a text processing application, and you want the text to be formatted in a given way, I. E indentation, justfication, left alignment or right alignment etc, and you can add some formatting tags in the raw text, and use regular expression to match the individual formatting tags, and replace them with the predefined formats.
The possibility is endless.