开发者

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 ?


  1. "Match and capture". /(?<=\s)(ever)(?=\s)/
  2. $line =~ s/ever\s+// will not remove word "ever", it will remove "ever" from "never". "\b" should be used here.
  3. 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜