in reply to Re: How to do regex backreferences within $variable replacement text?
in thread How to do regex backreferences within $variable replacement text?

Before seeing your code, I had tried something similar but limited only to $1. Now I've worked on it a bit more and came up with this. Just another WTDI.

use warnings; use strict; my $user_defined_string = "There's more than one way to do it (more th +an one)."; my $user_defined_search = '(more)(.*?)(one)'; my $user_defined_replace = '<b>$1</b>$2<b>$3</b>'; my (@subs) = $user_defined_string =~ /$user_defined_search/; for my $sub (1..@subs) { $user_defined_replace =~ s/\$$sub/$subs[$sub-1]/ge; } print "mangled replace: $user_defined_replace\n"; $user_defined_string =~ s/$user_defined_search/$user_defined_replace/g +e; print "after: $user_defined_string\n"; __OUTPUT__ mangled replace: <b>more</b> than <b>one</b> after: There's <b>more</b> than <b>one</b> way to do it (<b>more</b> t +han <b>one</b>).

As we can see, the user is expected to have a deep understanding of Perl regexes (non-greediness in this example) if she wants to do fancy stuff ;^).

--
David Serrano

  • Comment on Re^2: How to do regex backreferences within $variable replacement text?
  • Download Code

Replies are listed 'Best First'.
Re^3: How to do regex backreferences within $variable replacement text?
by Skeeve (Parson) on Sep 18, 2005 at 07:32 UTC
    This simply does not work!

    Replace the search text with "There's more than one way to do it (more or less than one)." and you'll see what I mean.

    you don't do any backreferencing but a simple replacement with the first strings found.

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print