免费注册 查看新帖 |

Chinaunix

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

查找替换中如何使用变量? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-09 12:04 |只看该作者 |倒序浏览
s_="AA BB CC DD";
$Content="BB";
请问要将BB替换成$mydate
s/bBBb/$mydate/能成功。但是s/$Content/$mydate/为什么不能成功?
替换中应该如何使用变量?

论坛徽章:
0
2 [报告]
发表于 2005-11-09 12:12 |只看该作者

回复 1楼 y_y_jun 的帖子

s/.../../e

论坛徽章:
0
3 [报告]
发表于 2005-11-09 12:27 |只看该作者

回复 1楼 y_y_jun 的帖子

仔细查看代码,发现是自己笔误。有个地方大小写弄错啦。*_*
结贴。

论坛徽章:
0
4 [报告]
发表于 2005-11-09 20:38 |只看该作者
偶在mailing list上看到关于一个关于regex的经典问答,把它贴出来:


提问:
if ($text =~ /(.*?($crlf))2(.*)/sm) {

Do I read this right?

the '2' is a repeat character of the second match
where match 1 is (.*?$crlf) and
match 2 is $crlf ?


回答:
That depends; we don't know the contents of $crlf (although we can
probably guess). But if $crlf has classes and/or logic, interpolating
the variable again will match any of the possiblilities, where the
backreference will only match the literal string previously matched.
Consider the following:

  ~/perl> perl -e'$abc="[abc]"; print "matched!n" if "aa" =~ /($abc)1/'
  matched!
  ~/perl> perl -e'$abc="[abc]"; print "matched!n" if "ab" =~ /($abc)1/'

but:

  ~/perl> perl -e'$abc="[abc]"; print "matched!n" if "ab" =~ /$abc$abc/'
  matched!


再问:
~/perl> perl -e'$abc="[abc]"; print "matched!n" if "ab" =~ /$abc$abc/'
matched!

Why this happen?what is the difference between "/($abc)1/" and "
/$abc$abc/"?Thanks.


再回答:
when a part of a regex is stored in a vairable, the contents of the
variable are interpolated before the regex is evaluated, so when the
match is performed,

   "ab" =~ /$abc/

becomes

  "ab" =~ /[abc]/

By the same token

  "ab" =~ /$abc$abc/

bcaomes

  "ab" =~ /[abc][abc]/

each class can match a or b or c, and the entire regex will match aa,
bb, cc, ab, ac, ba, bc, ca, or cb.

with

  /($abc)1/

however, 1 isn't evaluated until *after* the capturing parentheses do
their work, and 1 is replaced with whatever was captured--essentially
the value of $1 at whatever point the engine reaches that point in the
expression.

the variable is interpolated, so the expression becomes

  /([abc])1/

then the engine begins evaluating the expression. As soon as the
parentheses capture something, the engine goes through and replaces 1
with the literal string captured.

In out example then, ([abc]) matches "a" and stores the value "a" in
$1. Then all occurances of 1 are replaced with "a".

  /([abc])[abc]/

says "find me an a, b, or c, save it to $1,a dn find me another a, b, or c"

  /([abc])1/

says "find me an a, b, or c, save it to $1, and then find me another
of whatever it is that was just found"

Of course, this only matters if the captured value is the result of
some logic or class operation.

  /(ab)ab/

and

  /(ab)1/

and

  /(ab){2}/

are functionally equivalent, although the first one is more efficient
since it doesn't perform and capturing or substitution.


非常经典哈!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP