Regex 101 Discussion I8 – replace space count with spaces








Exercise I8 – 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 should end up with


    Text


*******


This is fairly straightforward. First, we need to match the space count “thingy”. We’ll use:


<?<Count>\d+)sp>


And then we’ll use a MatchEvaluator to do the replacement.


static public string Evaluator(Match match)

   int count = Int32.Parse(match.Groups[“Count”].Value); 
   return new String(‘ ‘, count);
}

Comments (1)

  1. Maurits says:

    typo… <?<Count>d+)sp> should be <(?<Count>d+)sp>