Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式的用法+_Regex_Perl - Fatal编程技术网

Regex 正则表达式的用法+

Regex 正则表达式的用法+,regex,perl,Regex,Perl,这是我的密码。我想打印出大写字母开头的所有行: while(<>){ if(/^[A-Z][a-z]+/){ print; } } 执行命令后: perl sc.pl test.txt Fred FreD 为什么弗雷德会被打印出来?我使用了[a-z]+,似乎+只匹配小写,而不是最后一个?字符串FreD匹配/^[a-z][a-z]+//因为[a-z]匹配F,[a-z]+匹配re 要获得所需的结果,还可以锚定正则表达式的结尾:/^[A-Z][A-Z]

这是我的密码。我想打印出大写字母开头的所有行:

while(<>){
    if(/^[A-Z][a-z]+/){
        print;
    }
}
执行命令后:

perl sc.pl test.txt

Fred
FreD
为什么弗雷德会被打印出来?我使用了[a-z]+,似乎+只匹配小写,而不是最后一个?

字符串FreD匹配/^[a-z][a-z]+//因为[a-z]匹配F,[a-z]+匹配re

要获得所需的结果,还可以锚定正则表达式的结尾:/^[A-Z][A-Z]+$/

另见

编辑:我现在看到@Biffen在评论中提供了相同的答案,对不起,

字符串FreD匹配/^[A-Z][A-Z]+/因为[A-Z]匹配F,[A-Z]+匹配re

要获得所需的结果,还可以锚定正则表达式的结尾:/^[A-Z][A-Z]+$/

另见

编辑:我现在看到@Biffen在评论中提供了相同的答案,抱歉

可能重复的
perl sc.pl test.txt

Fred
FreD