Perl的grep函数如何与正则表达式一起工作? [英] How does Perl's grep function work with a regex?

查看:170
本文介绍了Perl的grep函数如何与正则表达式一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的grep函数如何工作(!/ 0o1Iil] / 是做什么的?)

  @chars = grep!/ 0o1Iil] /,0..9,A..Z,a..z; 
使用Data :: Dumper;
print Dumper @chars;

在@chars中产生以下内容?

  $ VAR1 = 0; 
$ VAR2 = 1;
$ VAR3 = 2;
$ VAR4 = 3;
$ VAR5 = 4;
$ VAR6 = 5;
$ VAR7 = 6;
$ VAR8 = 7;
$ VAR9 = 8;
$ VAR10 = 9;
$ VAR11 ='A';
$ VAR12 ='B';
$ VAR13 ='C';
$ VAR14 ='D';
$ VAR15 ='E';
$ VAR16 ='F';
$ VAR17 ='G';
$ VAR18 ='H';
$ VAR19 ='I';
$ VAR20 ='J';
$ VAR21 ='K';
$ VAR22 ='L';
$ VAR23 ='M';
$ VAR24 ='N';
$ VAR25 ='O';
$ VAR26 ='P';
$ VAR27 ='Q';
$ VAR28 ='R';
$ VAR29 ='S';
$ VAR30 ='T';
$ VAR31 ='U';
$ VAR32 ='V';
$ VAR33 ='W';
$ VAR34 ='X';
$ VAR35 ='Y';
$ VAR36 ='Z';
$ VAR37 ='a';
$ VAR38 ='b';
$ VAR39 ='c';
$ VAR40 ='d';
$ VAR41 ='e';
$ VAR42 ='f';
$ VAR43 ='g';
$ VAR44 ='h';
$ VAR45 ='i';
$ VAR46 ='j';
$ VAR47 ='k';
$ VAR48 ='l';
$ VAR49 ='m';
$ VAR50 ='n';
$ VAR51 ='o';
$ VAR52 ='p';
$ VAR53 ='q';
$ VAR54 ='r';
$ VAR55 ='s';
$ VAR56 ='t';
$ VAR57 ='u';
$ VAR58 ='v';
$ VAR59 ='w';
$ VAR60 ='x';
$ VAR61 ='y';
$ VAR62 ='z';


解决方案

以下是 grep perldoc 。您示例中的语句使用 grep EXPR,LIST 语法,这意味着任何Perl表达式都可以取代 EXPR



grep将提供给它的列表,并仅返回EXPR为true的项。

这种情况下的EXPR是! / 0o1Iil] / (为了便于阅读,增加了空格),意思是这个项目是 not 匹配正则表达式 / 0o1Iil] / 0o1Iil] ),它们都被返回。



正如其他海报所提到的,正则表达式可能应该读取 / [0o1Iil] / ,这会删除可能会混淆的字符,例如0和o,1和I.这听起来对密码或序列号等有用。



顺便说一句,你可以将grep改写成更清晰的BLOCK格式,并将LIST施工显式:

pre $ code $ @chars = grep {!/ [0o1Iil] /}(0..9,'A'.. 'z','a'..'z');


How does the following grep function works (what does !/0o1Iil]/ do? )

@chars = grep !/0o1Iil]/, 0..9, "A".."Z", "a".."z"; 
use Data::Dumper; 
print Dumper @chars;

to produce the following in @chars?

$VAR1 = 0;
$VAR2 = 1;
$VAR3 = 2;
$VAR4 = 3;
$VAR5 = 4;
$VAR6 = 5;
$VAR7 = 6;
$VAR8 = 7;
$VAR9 = 8;
$VAR10 = 9;
$VAR11 = 'A';
$VAR12 = 'B';
$VAR13 = 'C';
$VAR14 = 'D';
$VAR15 = 'E';
$VAR16 = 'F';
$VAR17 = 'G';
$VAR18 = 'H';
$VAR19 = 'I';
$VAR20 = 'J';
$VAR21 = 'K';
$VAR22 = 'L';
$VAR23 = 'M';
$VAR24 = 'N';
$VAR25 = 'O';
$VAR26 = 'P';
$VAR27 = 'Q';
$VAR28 = 'R';
$VAR29 = 'S';
$VAR30 = 'T';
$VAR31 = 'U';
$VAR32 = 'V';
$VAR33 = 'W';
$VAR34 = 'X';
$VAR35 = 'Y';
$VAR36 = 'Z';
$VAR37 = 'a';
$VAR38 = 'b';
$VAR39 = 'c';
$VAR40 = 'd';
$VAR41 = 'e';
$VAR42 = 'f';
$VAR43 = 'g';
$VAR44 = 'h';
$VAR45 = 'i';
$VAR46 = 'j';
$VAR47 = 'k';
$VAR48 = 'l';
$VAR49 = 'm';
$VAR50 = 'n';
$VAR51 = 'o';
$VAR52 = 'p';
$VAR53 = 'q';
$VAR54 = 'r';
$VAR55 = 's';
$VAR56 = 't';
$VAR57 = 'u';
$VAR58 = 'v';
$VAR59 = 'w';
 $VAR60 = 'x';
 $VAR61 = 'y';
 $VAR62 = 'z';

解决方案

Here's the grep perldoc. The statement in your example is using the grep EXPR,LIST syntax, which means any Perl expression can take the place of EXPR.

grep takes the list provided to it, and returns only the items where EXPR is true.

EXPR in this case is ! /0o1Iil]/ (space added for readability) which means "this item is not matched by the regex /0o1Iil]/. Since none of those items are matched by that regular expression (none of them contain the string 0o1Iil]) they are all returned.

As other posters have mentioned, the regex was probably supposed to read /[0o1Iil]/, which would remove characters that could be confused, e.g. 0 and o, 1 and I. This sounds useful for passwords or serial numbers, etc.

Btw, you could rewrite the grep into the clearer BLOCK form, and make the LIST construction explicit:

@chars = grep { ! /[0o1Iil]/ } (0..9, 'A'..'Z', 'a'..'z');

这篇关于Perl的grep函数如何与正则表达式一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆