On Thu, Oct 18, 2001 at 07:00:38PM +0200, Pascal Oberndoerfer wrote:
> Hello.
>
> I got into trouble using grouping and variables in regular expressions.
>
> Given this scenario:
>
> s/search_for(but_preserve)/replace_with $1/;
>
> I can also do:
>
> $pattern = 'search_for(but_preserve)';
> $replace = 'replace_with';
> s/$pattern/$replace $1/;
>
> But I am unable to do:
>
> $pattern = 'search_for(but_preserve)';
> $replace = 'replace_with $1';
> s/$pattern/$replace/;
>
>
> It would be very helpful if I could to this as the grouping in the
> "$pattern" varies as well as the placement of the corresponding
> variables $1, $2, ...
>
> I tried to do it with eval() -- without any success. Does eval() make
> any sense here at all?
Yes, but as /e rather than eval().
s/$pattern/qq{"$replace"}/ee;
You need two levels of eval to get the two levels of interpolation.
Ronald