开发者

Regular expression lookahead and/or lookbehind wrong in that

开发者 https://www.devze.com 2023-03-17 17:49 出处:网络
$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.

$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

精彩评论

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