免费注册 查看新帖 |

Chinaunix

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

请教一个模式匹配的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-14 10:57 |只看该作者 |倒序浏览
my $x='Perl is free.';
my $y='ManagerWare costs $99.95.';
foreach ($x,$y) {
        /^(\w+) (?:is|(costs)) (?(2)(\$\d+)|\w+)/;
        if($3) {
                print "$1 costs money.\n";
        } else {
                print "$1 doesn't cost money.\n";
        }
}
上面这个例子中,模式?(2)这个是什么意思?

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2013-05-14 11:27 |只看该作者
perlre 摘录
(?(condition)yes-pattern|no-pattern)
(?(condition)yes-pattern)

Conditional expression. Matches yes-pattern if condition yields a true value, matches no-pattern otherwise. A missing pattern always matches.

Here's a summary of the possible predicates:

    (1) (2) ...

    Checks if the numbered capturing group has matched something.
    (<NAME>) ('NAME')

    Checks if a group with the given name has matched something.
    (?=...) (?!...) (?<=...) (?<!...)

    Checks whether the pattern matches (or does not match, for the '!' variants).
    (?{ CODE })

    Treats the return value of the code block as the condition.

论坛徽章:
0
3 [报告]
发表于 2013-05-14 13:28 |只看该作者
多谢版主指点!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP