Show what a substitution is doing ?

W

Willem

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
 
S

sln

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__
 
W

Willem

Marc Girod 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
 
W

Willem

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
 
W

Willem

(e-mail address removed) 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top