Regular expression lookahead and/or lookbehind wrong in that
$line = 'bob never ever said every reverie was good';
Looking: Match and capture ONLY the word 'ever'. Do so using lookahead and/or lookbehind assertions.
if ( $line =~ /(?<=\s开发者_开发知识库)ever(?=\s)/) {
print "matched ";
}
substituting: Remove the word 'ever' and the space after it from the line using any mechanism you'd like.
$line =~ s/ever\s+//;
print $line ;
Extra-Credit: Get the character offset into the string of the word 'ever' using any mechanism you'd like.
my $result = index($line,'ever');
print $result;
I have wrote the exam. but i am not passs through. What is wrong in these answers ?
- "Match and capture". /(?<=\s)(ever)(?=\s)/
- $line =~ s/ever\s+// will not remove word "ever", it will remove "ever" from "never". "\b" should be used here.
- Same as 2, would find "ever" in "never", so you should search for " ever " instead and add 1 (because of adding space). You can add another 1 if you consider that 1st character in string has offset 1.
精彩评论