perl-可选修饰符

[b][size=large][align=center]perl-可选修饰符[/align][/size][/b]

[b][size=large]可选修饰符[/size][/b]

这些修饰符可有可无,有时候称为开关,放在正则表达式结尾的界定符右边,并改变正则表达式的默认行为。
/i 进行大小无关的匹配
/s 匹配任意字符
/x 加入空白

实例:
1) /i修饰符可以让你在进行模式匹配时不区分大小写。

#!/perl
use strict;

print "Would you like to play a game?";
chomp($_ = <STDIN>);
if (/YES/i){
print "You input the string : $_";
}
#end


2)用/s来匹配任意字符,默认情况下(.)号无法匹配换行符,这对大多数单行匹配的情况是符合的,但是如果字符串中有换行符时,用/s就可以完成匹配。


#!/perl
use strict;

my ($var);
$var = "I saw Barney\ndown at the bowling alley\nwith fred\nlast night.\n";
#if ($var_sub =~ /Barney.*fred/s){
if ($var =~ /Barney.*fred/s){
print "That matching mentions fred Barney.";
}
#end



3) 用/x加入空白
在模式里随意加入空白,使它阅读更容易:
/-? \d+\.?\d*/
/ -? \ d+ \ .? \ d* /
加入空白后更易于阅读。

附加:
. Match any character
\w Match "word" character (alphanumeric plus "_")
\W Match non-word character
\s Match whitespace character
\S Match non-whitespace character
\d Match digit character
\D Match non-digit character
\t Match tab
\n Match newline
\r Match return
\f Match formfeed
\a Match alarm (bell, beep, etc)
\e Match escape
\021 Match octal char ( in this case 21 octal)
\xf0 Match hex char ( in this case f0 hexidecimal)


You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here's where you start getting some power:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值