- 论坛徽章:
- 0
|
本帖最后由 david_95 于 2015-07-02 14:22 编辑
1, 正则表达pattern 可以使用用变量, 但是如果变量中含有特殊字符(比如 [ . * \ ...),该怎么办?
比如我想搜索字符串$str 是否有 [ , 我写以下程序, 第一个if block 就没有生效,只有第二个查出了
sub testReg2{
my $str="sdfsff\s[sdfsdf";
my $mat="[";
# this if block not work as expected
if($str=~m/\$mat/g){
print "find out with variable \n";
}
# this if block work
if($str=~m/\[/g){
print "find out with constant\n";
}
}
2。 正则表达式 . 代表任意字符, 我想搜索3个字符的全排列, 写了以下程序,但是它没有全组合(用全组合不恰当,应该是依次排列的3个字符串),只是从第一个字符开始,依次找到3个字符
我想 bcd cde ...igj 也应该是符合表达式的,为啥没找出呢? 我要怎么写才能用正则匹配写出 全部依次排列的3个字符串 呢?
sub testReg{
my $str="abcdefghigj";
my @tsr=$str=~/.../g;
print "@tsr \n";
}
以上程序输出 abc def ghi |
|