Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Show what a substitution is doing ?

Reply
Thread Tools

Show what a substitution is doing ?

 
 
Willem
Guest
Posts: n/a
 
      03-24-2010
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
 
Reply With Quote
 
 
 
 
Marc Girod
Guest
Posts: n/a
 
      03-24-2010
On Mar 24, 6:35*pm, Willem <wil...@turtle.stack.nl> 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
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      03-24-2010
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__

 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      03-24-2010
Marc Girod wrote:
) On Mar 24, 6:35?pm, Willem <wil...@turtle.stack.nl> 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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      03-25-2010
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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      03-25-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
asp:checkbox hide/show text/fields when it's clicked without doing a postback? UJ ASP .Net 4 04-24-2009 04:50 PM
TO show or NOT to show Matt ASP .Net 1 05-02-2005 09:07 PM
Questions regarding doing digital slide show on TV vs. Computer mon. Caloonese Digital Photography 4 06-06-2004 08:30 PM
to show image or to show flash Disco Octopus HTML 7 05-14-2004 06:27 AM
Command "show run" does not show all interfaces Peter Jonas Cisco 3 01-05-2004 12:13 AM



Advertisments