Regex 101 Exercise 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


 


 

Comments (16)

  1. using System;

    using System.Text.RegularExpressions;

    class Rx

    {

           public static void Main(string[] args)

           {

                   string text = "<8sp>hello<1sp>world";

                   string[] ar = Regex.Split(text, "<([0-9]?)sp>");

                   text = "";

                   for(int i=1; i<ar.Length; i=i+2)

                   {

                           text+=spaces(int.Parse(ar[i]))+ar[i+1];

                   }

                   Console.WriteLine(text);

           }

           static string spaces(int s)

           {

                   string foo = "";

                   while(s–>0)

                   {

                           foo += " ";

                   }

                   return foo;

           }

    }

    Output:

    jozjan@black:~/temp$ mcs rx.cs

    jozjan@black:~/temp$ ./rx.exe

           hello world

    Other ideas?

  2. A slightly different approach:

    using System;

    using System.Text.RegularExpressions;

    class RegExample

    {

    [STAThread]

    static void Main(string[] args)

    {

    RegExample rex = new RegExample();

    string text = "<10sp>Hello,<1sp>World!<2sp>With<1sp>Regular<1sp>Expressions!";

    MatchEvaluator myEvaluator = new MatchEvaluator(rex.spaces);

    Console.WriteLine(Regex.Replace(text,"<([0-9])*sp>", myEvaluator));

    }

    public string spaces(Match m)

    {

    string tmp = "";

    int i= Convert.ToInt32(m.Value.Split("<sp>".ToCharArray())[1]);

    for(int x=0;x<i;++x)

    {

    tmp += " ";

    }

    return tmp;

    }

    }

    output:

    >RegExample.exe

             Hello, World!  With Regular Expressions!

  3. Maurits says:

    using System;

    using System.Text;

    using System.Text.RegularExpressions;

    class RegExSample

    {

    static string ExpandSpaces(string s)

    {

    string pattern =

    @"

    < # start of tag

    (d+) # one or more digits

    sp # literal ‘sp’

    > # end of tag

    ";

    return Regex.Replace(

    s,

    pattern,

    new MatchEvaluator(

    RegExSample.ExpandSpacesByMatch

    ),

    RegexOptions.IgnorePatternWhitespace

    );

    }

    static string ExpandSpacesByMatch(Match m)

    {

    string sCount = m.Groups[1].ToString();

    int iCount = int.Parse(sCount);

    string s = new string(‘ ‘, iCount);

    return s;

    }

    static void Main(string[] args)

    {

    if (args.Length == 0)

    {

    run("a<1sp>b");

    }

    foreach (string s in args)

    {

    run(s);

    }

    }

    static void run(string s)

    {

    System.Console.WriteLine();

    System.Console.WriteLine("original: "" + s + """);

    System.Console.WriteLine("modified: "" + ExpandSpaces(s) + """);

    }

    }

    >RegExSample "a<1sp>b<2sp>c" "a<15sp>"

    original: "a<1sp>b<2sp>c"

    modified: "a b  c"

    original: "a<15sp>"

    modified: "a               "

  4. PhilipTaron says:

    Using the interpolation module from http://search.cpan.org/~jenda/Interpolation-0.70.1/Interpolation.pm

    perl -pe "use Interpolation n => sub { ‘ ‘ x $_[0] }; s/[<](d+)sp[>]/$n{$1}/g"

    Yay for Perl.

  5. Maurits says:

    Grrr… indentation is left as an exercise.

  6. Maurits says:

    Or just use /e 🙂

    perl -e "$_ = shift; s/<(d+)sp>/’ ‘ x $1/eg; print;" "a<1sp>b<2sp>c"

  7. Maurits says:

    Or if you don’t like /e there’s eval-by-anonymous-array-inclusion:

    perl -e "$_ = shift; s/<(d+)sp>/@{ [ ‘ ‘ x $1 ] }/g; print;" "a<0sp>b<1sp>c<2sp>d"

    Or even

    perl -e "$_ = shift; s/<(d+)sp>/@{ [ map { ‘ ‘ } (1 .. $1) ] }/g; print;" "a<0sp>b<1sp>c<2sp>d"

    (Yuck…)

  8. PhilipTaron says:

    Woah, /e is _cool_.

    perl -pe "s/<(d+)sp>/’ ‘x$1/eg"

  9. kbiel says:

    I think that Maurits’ answer(s) covers it.  I couldn’t come up with a more succinct way of writing it.

  10. Sheva says:

    Regex regex = new Regex(@"<(d+)sp>", RegexOptions.Compiled | RegexOptions.IgnoreCase);

               Console.WriteLine("Type in the HTML text you want to process:");

               String inputHtml = Console.ReadLine();

               String resultHtml = regex.Replace(inputHtml, delegate(Match match)

               {

                   return new String(‘ ‘, Int32.Parse(match.Groups[1].Value));

               });

    So far, mine is the shortest one, so far so good:-)

    Sheva

  11. Maurits says:

    No official answer?