免费注册 查看新帖 |

Chinaunix

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

perl中的$_ $1、$2怎么识别 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-30 20:19 |只看该作者 |倒序浏览
open(F,">$dir/duxiu_cookie");
                if($cookies=~/(AID_dsr=.*?);/){print F "$1\n";}
                if($cookies=~/(userid_dsr=.*?);/){print F "$1\n";}
                if($cookies=~/(userName_dsr=.*?);/){print F "$1\n";}
                if($cookies=~/(enc_dsr=.*?);/){print F "$1\n";}
                close(F);

                                 my $yrno=0,$yrstart=-1,$yrend=-1;
                         while($request =~ s/(AND|OR|NOT)*\s*WYR = \(\s*(.*?)\s*\)//){
                $yr=$2;$yrno++;

这两个里面的$1和$2是怎么确定的呢,前面没有出现过呀?


我刚学,所以问的问题比较菜,别见笑。

论坛徽章:
0
2 [报告]
发表于 2010-03-30 21:16 |只看该作者
数括号

论坛徽章:
0
3 [报告]
发表于 2010-03-30 21:38 |只看该作者
print出来看看是啥值,就清楚了。

论坛徽章:
0
4 [报告]
发表于 2010-03-30 21:40 |只看该作者
回复 1# jinger18


   
Grouping Matching

From a regular-expression point of view, there is no difference between except, perhaps, that the former is slightly clearer.

$string =~ /(\S+)\s+(\S+)/;

and

$string =~ /\S+\s+\S+/;

However, the benefit of grouping is that it allows us to extract a sequence from a regular expression. Groupings are returned as a list in the order in which they appear in the original. For example, in the following fragment we have pulled out the hours, minutes, and seconds from a string.

my ($hours, $minutes, $seconds) = ($time =~ m/(\d+)\d+)\d+)/);

As well as this direct method, matched groups are also available within the special $x variables, where x is the number of the group within the regular expression. We could therefore rewrite the preceding example as follows:

$time =~ m/(\d+)\d+)\d+)/;
my ($hours, $minutes, $seconds) = ($1, $2, $3);

When groups are used in substitution expressions, the $x syntax can be used in the replacement text. Thus, we could reformat a date string using this:

#!/usr/bin/perl

$date = '03/26/1999';
$date =~ s#(\d+)/(\d+)/(\d+)#$3/$1/$2#;

print "$date";

This will produce following result
1999/03/26

论坛徽章:
0
5 [报告]
发表于 2010-03-30 21:54 |只看该作者
简单的说,就是捕获正则表达式的分组。
正则表达式用()进行分组。

论坛徽章:
0
6 [报告]
发表于 2010-03-30 22:07 |只看该作者
哦,好像看懂了。
呵呵,感觉perl挺难的,照着教程学了一遍感觉还是不会编程

论坛徽章:
0
7 [报告]
发表于 2010-03-30 22:24 |只看该作者
哦,好像看懂了。
呵呵,感觉perl挺难的,照着教程学了一遍感觉还是不会编程
jinger18 发表于 2010-03-30 22:07



    看看置顶贴里推荐的书吧,那样事半功倍

论坛徽章:
0
8 [报告]
发表于 2010-03-31 09:54 |只看该作者
恩,会的,谢谢。
现在是时间紧迫,必须先编出个程序来

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
9 [报告]
发表于 2010-03-31 10:39 |只看该作者
哦,好像看懂了。
呵呵,感觉perl挺难的,照着教程学了一遍感觉还是不会编程
jinger18 发表于 2010-03-30 22:07



    你的问题在于你不会编程,而不是不会用 Perl, 这和 Perl 难不难完全扯不上关系。

论坛徽章:
0
10 [报告]
发表于 2010-03-31 12:06 |只看该作者
回复 9# MMMIX


    恩,我同意您的说法。但没办法,在实习考核中要一周编个程序,为了找工作,只能先走步捷径了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP