我是想传递一个正则字串比如传给parttern或replacement都行,但是如果直接把向后引用 ' $1'  $2...等等 传给  s/
parttern/replacement/  比如 s/$parttern/$replacement
则 $1 不起作用,或者提前解释展开了,或者单纯认为是字符串'$1'

网上找的只是 eval方法 不知道是不是标准方法

On 2月25日, 上午9时43分, Michael Zeng <[email protected]> wrote:
>  没看懂你在干啥,能举个例子么
>
> 感觉这样写 $1 应该不好
>
> On 2/25/09, buxieyigu <[email protected]> wrote:
>
>
>
>
>
> > sub regfromvar
> > {
> >        my $str1="requestform";
> >        my $str2=$str1;
> >        my $parttern = "request(form)";
> >        my $replacement = '$1';
> >        print $str1,"\n";
> >        my $suc;
> >        eval '$suc=$str1 =~ s/'.$parttern.'/'.$replacement.'/igxm';
> >        if($suc)
> >        {
> >                        print $str1,"\n";
> >        }
>
> >        print $str2,"\n";
> >        if($str2 =~ s/$parttern/$1/igxm)
> >        {
> >                        print $str2,"\n";
> >        }
> > }
>
> > regfromvar();
>
> > 这样好像能解决问题,但是感觉不太对劲,有没有更标准的方法,难道正则不提供 $1 的转换么,传给他字符串就只能当作字符串处理。
>
> --
>             Yours Sincerely
>                     Zeng Hong
--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复