Share via


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

  • Anonymous
    February 13, 2006
    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?

  • Anonymous
    February 13, 2006
    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!

  • Anonymous
    February 13, 2006
    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               "

  • Anonymous
    February 13, 2006
    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/<sp[>]/$n{$1}/g"

    Yay for Perl.

  • Anonymous
    February 13, 2006
    Grrr... indentation is left as an exercise.

  • Anonymous
    February 13, 2006
    Or just use /e :)

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

  • Anonymous
    February 13, 2006
    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...)

  • Anonymous
    February 13, 2006
    Indented version of C# above:
    http://www.geocities.com/mvaneerde/expand-spaces.txt

  • Anonymous
    February 13, 2006
    Woah, /e is cool.

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

  • Anonymous
    February 13, 2006
    I think that Maurits' answer(s) covers it.  I couldn't come up with a more succinct way of writing it.

  • Anonymous
    February 14, 2006
    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

  • Anonymous
    February 14, 2006
    TrackBack From:http://sheva.cnblogs.com/archive/2006/02/15/331343.html

  • Anonymous
    February 15, 2006
    TrackBack From:http://sheva.cnblogs.com/archive/0001/01/01/331343.html

  • Anonymous
    February 24, 2006
    No official answer?

  • Anonymous
    June 02, 2009
    PingBack from http://woodtvstand.info/story.php?id=85632

  • Anonymous
    June 07, 2009
    PingBack from http://greenteafatburner.info/story.php?id=2270