正则表达式前视和/或后视错误

发布于 2024-11-19 00:41:52 字数 480 浏览 7 评论 0原文

$line = 'bob never never said every reverie is good';

查找:仅匹配并捕获单词“ever”。使用前瞻和/或后瞻断言来执行此操作。

if ( $line =~ /(?<=\s)ever(?=\s)/) {
 print "matched ";
}

替换:使用您想要的任何机制从行中删除单词“ever”及其后面的空格。

$line =~ s/ever\s+//;
print $line ;

Extra-Credit:使用您想要的任何机制将字符偏移量获取到单词“ever”的字符串中。

my $result = index($line,'ever');
print $result;

我已经写了考试。但我不是穿越者。这些答案有什么问题?

$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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2024-11-26 00:41:52
  1. “匹配并捕获”。 /(?<=\s)(ever)(?=\s)/
  2. $line =~ s/ever\s+// 不会删除单词“ever”,它将删除“ever” ”源自“从不”。这里应该使用“\b”。
  3. 与 2 相同,会在“never”中找到“ever”,因此您应该搜索“ever”并添加 1(因为添加了空格)。如果您认为字符串中的第一个字符的偏移量为 1,则可以再添加 1。
  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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文