免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1368 | 回复: 2
打印 上一主题 下一主题

perl 老版本模式分组问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-22 09:23 |只看该作者 |倒序浏览


perl 版本5.8.8

文本内容

<param name="UserPassword" type="encrypt">b273290137C725168F52F3D7E1EA8670</param>


需要将红色部分的文字替换为新的字符串

本人菜鸟写的perl语句为:

#B="UserPassword"
#C="encrypt"
perl -ne 'use 5.010;s/(<param name="'$B'" type="'$C'">)(?:.*?)(<\/param>)/\1'$A'\2/;print $_'  xxx.xml

当变量$A的值为 数字开头的字符串时,就会出问题,例如A="123456" 这类字符串时,perl无法得出正确的结果

由于perl版本较低,分组模式引用又不能使用\g{1} 这种方法

求助各位高人,如何解决该问题?

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2012-10-22 12:17 |只看该作者
回复 1# romanticsong

1. shell variable use %ENV, ex: %ENV{"B"}
$ export B="UserPassword"
$ perl -e 'print "shell \$B=$ENV{B}\n"'   
shell $B=UserPassword

2. \1 change to $1 ==> ${1}

perldoc perlre
   Warning on \1 Instead of $1
       Some people get too used to writing things like:

           $pattern =~ s/(\W)/\\\1/g;

       This is grandfathered (for \1 to \9) for the RHS of a substitute to
       avoid shocking the sed addicts, but it's a dirty habit to get into.
       That's because in PerlThink, the righthand side of an "s///" is a
       double-quoted string.  "\1" in the usual double-quoted string means a
       control-A.  The customary Unix meaning of "\1" is kludged in for
       "s///".  However, if you get into the habit of doing that, you get
       yourself into trouble if you then add an "/e" modifier.

           s/(\d+)/ \1 + 1 /eg;            # causes warning under -w

       Or if you try to do

           s/(\d+)/\1000/;

       You can't disambiguate that by saying "\{1}000", whereas you can fix it
       with "${1}000".
  The operation of interpolation should not be confused
       with the operation of matching a backreference.  Certainly they mean
       two different things on the left side of the "s///".

论坛徽章:
0
3 [报告]
发表于 2012-10-22 13:01 |只看该作者
回复 2# jason680


perl -ne 's/(<param name="'$B'" type="'$C'">)(?:.*?)(<\/param>)/${1}'$A'${2}/;print $_'  xxx.xml


感谢大牛,将\1 修改为 ${1}解决了




   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP