in reply to Substituting in the substitution!

this'll do it
#!perl -wl use strict; $_ = "[ABC AB AB12/83]"; print "I have a string like this\n$_"; s{\[([A-Z_]+)\s([A-Z_]+)\s([A-Z_]+)(\d+)/(\d+)\]}{$1_$2_$3$4_$5}; print "I would like to end up with\n$_";

perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

Replies are listed 'Best First'.
Re: Re: Substituting in the substitution!
by Mondongo (Beadle) on May 06, 2004 at 16:29 UTC
    I didn't explain myself quite right.

    The "/" can be in any part of the string, so a new regexp won't fix it. What I want is to capture four clusters and then, when substituting, replace the "/" inside the clusters.

    Right now, I did this:
    $do_the_change_mon = sub { $thingy = shift; $thingy =~ tr|/|_|; return $thingy; } $expr = s| \[([A-Z/]+)\s([A-Z/]+)\s([A-Z/]+)\s([A-Z/]+)\s\] |&$do_the_change_mon("$1_$2_$3_$4)|xe;

    Thank you all for your kind answers, monks!

      That's not bad. Consider this. I rolled your A-Z/ loop up and then did the substitution on a copy inside the RHS.

      $expr =~ s(\[((?:[A-Z/]+)\s){4,4})\]){ my $match = $_; $match =~ s([/\s])(_)g; $match; }ge;