|
1 simple
print "It matches\n" if "hello world" =~ /world/;
print "It doesn't match\n" if "hello world" !~ /word/;
$_ = "hello world";
print "it matches\n" if m{world};
print "it matches\n" if m!world!;
print "it matches\n" if m(world);
print "it matches\n" if m[world];
2 metacharacter
. | 任意一个字符\n除外 | + | 重复>=1次 | * | 重复>=0次 | ? | 重复<=1次 | $ | 行尾 | [] | 字符类,匹配其中任意一个 | () | 组,在组中^$表行首行尾 | \d | [0-9] | \D | [^0-9] | \s | 空白字符 | \S | [^\s] | \w | [0-9a-zA-Z] | \W | [^\w] | \b | 单词边界 | //i | case-insensitive | //g | 多次匹配,函数pos记录其位置 | s/// | replace | s///r | 返回替代结果但并不修改原来的值$_或用=~绑定的值 | s///e | 指明计算后的结果替代匹配的字符串 |
$x = "cat dog house"; # 3 words
while ($x =~ /(\w+)/g){
print "Word is $1, ends at position ", pos $x, "\n";
}
# reverse all the words in a string
$x = "the cat in the hat";
$x =~ s/(\w+)/reverse $1/ge; # $x contains "eht tac ni eht tah"
# convert percentage to decimal
$x = "A 39% hit rate";
$x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"
Author: visaya fan <visayafan[AT]gmail.com or visayafan[AT]163.com>
Date: 2011-10-27 22:02:37
HTML generated by org-mode 6.33x in emacs 23 |
|
|