Reputation: 393
Here is my code. I want to print out all the lines at the beginning of capital:
while(<>){
if(/^[A-Z][a-z]+/){
print;
}
}
test.txt
:
Fred
fred
FRED
FreD
After I execute the command:
perl sc.pl test.txt
Fred
FreD
Why the FreD
will be printed out? I have use [a-z]+
, it seems that the +
only match the lower case expect the last one?
Upvotes: 1
Views: 127
Reputation: 3013
The string FreD
matches /^[A-Z][a-z]+/
because [A-Z]
matches F
and [a-z]+
matches re
.
To get the desired result, anchor the end of the regex as well: /^[A-Z][a-z]+$/
.
See also perlretut.
(Edit: I see now that @Biffen has provided the same answer in the comments, sorry)
Upvotes: 8