Show what a substitution is doing ?

Discussion in 'Perl Misc' started by Willem, Mar 24, 2010.

  1. Willem

    Willem Guest

    Hello,

    For a program I'm writing that does a few regex-based substitutions
    in a large file, I would like to see exactly what substitutions are
    being done. I.E. Which strings were matched, and what they were
    replaced by.

    Or, in code: if I do:

    $content =~ s/<add key="(.*?).foobar.\d+" value="Foo=.*?;(.*?"/>)/
    <add key="$1.foobar.10" value="Foo=20;$2/g;

    I want to display stuff like:
    Substitution: '<add key="one.foobar.12" value="Foo=15;Bar=3"/>'
    => '<add key="one.foobar.10" value="Foo=20;Bar=3"/>'
    Substitution: '<add key="two.foobar.12" value="Foo=15;Bar=8"/>'
    => '<add key="two.foobar.10" value="Foo=20;Bar=8"/>'

    I have already succeeded in doing this by making a loop around
    while ($content =~ /.../g)

    where I used the @- and @+ arrays to get at the matches, and then
    manually fill in the $1 .. $x variables.
    That's pretty complicated, however, and also the actual substitution is
    pretty hairy too (I had to do another s/// with the strings I just created
    and displayed).

    Is there an easier way to get at what a substitution is doing ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Mar 24, 2010
    #1
    1. Advertising

  2. Willem

    Marc Girod Guest

    On Mar 24, 6:35 pm, Willem <> wrote:

    > Is there an easier way to get at what a substitution is doing ?


    Run under the debugger, with an action to print the values before and
    after?

    Marc
     
    Marc Girod, Mar 24, 2010
    #2
    1. Advertising

  3. Willem

    Guest

    On Wed, 24 Mar 2010 18:35:05 +0000 (UTC), Willem <> wrote:

    >Hello,
    >
    >For a program I'm writing that does a few regex-based substitutions
    >in a large file, I would like to see exactly what substitutions are
    >being done. I.E. Which strings were matched, and what they were
    >replaced by.
    >
    >Or, in code: if I do:
    >
    > $content =~ s/<add key="(.*?).foobar.\d+" value="Foo=.*?;(.*?"/>)/
    > <add key="$1.foobar.10" value="Foo=20;$2/g;
    >
    >I want to display stuff like:
    > Substitution: '<add key="one.foobar.12" value="Foo=15;Bar=3"/>'
    > => '<add key="one.foobar.10" value="Foo=20;Bar=3"/>'
    > Substitution: '<add key="two.foobar.12" value="Foo=15;Bar=8"/>'
    > => '<add key="two.foobar.10" value="Foo=20;Bar=8"/>'
    >
    >I have already succeeded in doing this by making a loop around
    > while ($content =~ /.../g)
    >
    >where I used the @- and @+ arrays to get at the matches, and then
    >manually fill in the $1 .. $x variables.
    >That's pretty complicated, however, and also the actual substitution is
    >pretty hairy too (I had to do another s/// with the strings I just created
    > and displayed).
    >
    >Is there an easier way to get at what a substitution is doing ?
    >
    >
    >SaSW, Willem


    Probably the literals '.' and '/' should be escaped.
    I'm suprised it worked.

    This is one way, probably more ways.
    -sln
    --------------

    use strict;
    use warnings;

    my $tmp;
    my $content = qq{
    <add key="one.foobar.12" value="Foo=15;Bar=3"/>
    <add key="two.foobar.12" value="Foo=15;Bar=8"/>
    };

    $content =~ s/(<add key=".*?\.foobar\.)(\d+)(" value="Foo=)(.*?)(;.*?"\/>)/
    $tmp = $1.'10'.$3.'20'.$5;
    print "Substitution: '$1$2$3$4$5'\n => '$tmp'\n";
    $tmp/eg;

    print $content;

    __END__
     
    , Mar 24, 2010
    #3
  4. Willem

    Willem Guest

    Marc Girod wrote:
    ) On Mar 24, 6:35?pm, Willem <> wrote:
    )
    )> Is there an easier way to get at what a substitution is doing ?
    )
    ) Run under the debugger, with an action to print the values before and
    ) after?

    1 - I want just the bits that are substituted, although that
    may very well be possible in the debugger ?
    2 - This is for a user app, and I want the user to see the changes.

    Especially #2 rules out using the debugger.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Mar 24, 2010
    #4
  5. Willem

    Willem Guest

    Ben Morrow wrote:
    ) It shouldn't need to be complicated.
    )
    ) while ($content =~ /foo(\d+)/g) {
    ) my ($start, $end) = ($-[0], $+[0]);
    ) # this qq// should contain exactly what you would have put
    ) # in the RHS of the s///
    ) my $after = qq/bar$1/;
    ) my $before = substr $content, $start, $end, $after;
    ) }

    I did something similar. However, because I didn't want to duplicate this
    code 5 times, I put it into a function, and that meant hand-parsing for $1,
    $2, etc. variables. Which made it quite complicated.

    However, the s///e solution mentioned crossthread had good potential:

    $content =~ s/<complicated expression (with parens)>/
    func("<Another expression with $1 and stuff>")/ge;

    sub func { print "Sub '$&' by '$_[0]'"; $_[0]; }

    Which works like a charm.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Mar 25, 2010
    #5
  6. Willem

    Willem Guest

    wrote:
    ) $content =~ s/(<add key=".*?\.foobar\.)(\d+)(" value="Foo=)(.*?)(;.*?"\/>)/
    ) $tmp = $1.'10'.$3.'20'.$5;
    ) print "Substitution: '$1$2$3$4$5'\n => '$tmp'\n";
    ) $tmp/eg;

    Yep, thanks, that worked quite well. (I put the expression into
    a function, and used $& instead of $1$2$3$4$5, though.)


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Mar 25, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt

    TO show or NOT to show

    Matt, May 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    388
  2. Disco Octopus

    to show image or to show flash

    Disco Octopus, May 13, 2004, in forum: HTML
    Replies:
    7
    Views:
    2,431
    Toby A Inkster
    May 14, 2004
  3. Replies:
    1
    Views:
    584
    Evertjan.
    Sep 30, 2005
  4. UJ
    Replies:
    4
    Views:
    46,536
    pamam
    Apr 24, 2009
  5. Replies:
    1
    Views:
    178
    Evertjan.
    Sep 30, 2005
Loading...

Share This Page