正则表达式 - 与条件环视匹配?

发布于 2024-11-09 10:14:11 字数 393 浏览 7 评论 0原文

我想使用环视来匹配字符串的一部分,但前提是该行中不包含其他单词。

一些熊在树林里生活和进食。

在上面的行中,我想找到“ eat in the ”(在“live and”和“woods”之间):

/(?<=\blive and\b).*?(?=\bwoods\b)/

但前提是“bears”不在行中,无论是在环视之后、之前还是之间 :

不应返回匹配项的其他行示例包括

有些动物生活和进食 树林,像熊一样。

一些动物以熊为食 树林里。

我如何将此条件添加到我的正则表达式中?

i would like to match a part of the string using lookarounds, but only if other words are not contained in the line.

Some bears live and eat in the woods.

in the above line, i'd like to find " eat in the " (between "live and" & "woods):

/(?<=\blive and\b).*?(?=\bwoods\b)/

but only if "bears" isn't apart of the line, either after, before or between the lookarounds.

other examples of lines that should not return a match are:

Some animals live and eat in the
woods, like bears.

Some animals live and eat bears in
the woods.

how can i add this condition to my regular expression?

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

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

发布评论

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

评论(3

寻找一个思念的角度 2024-11-16 10:14:11

这是有问题的,因为大多数风格不支持可变长度后视,因此您无法检查整行。一种简单的方法是匹配整行而不是使用环视:

^(?!.*\bbears\b).*?\blive and\b(.*?)\bwoods\b

在这里,之前的整个匹配是第一个捕获组。根据您的使用情况,替换此文本可能会不太方便。确保使用多行标志 (/m),而不是设置单行标志(dot-all 或 /s)。

工作示例: http://rubular.com/r/TuADb2vB4w

请注意如果你能通过两步解决问题,问题就变得非常简单:过滤掉带有 \bbears\b 的行,并匹配想要的字符串。

This is problematic because most flavors do not support a variable length look-behind, so you can't check the whole line. A simple approach is to match the whole line instead of using lookarounds:

^(?!.*\bbears\b).*?\blive and\b(.*?)\bwoods\b

Here, what previously was the whole match is the first capturing group. Depending on your you use it, it may make replacing this text a little less convenient. Make sure to use the muliline flag (/m), and not the set the single-line flag (dot-all, or /s).

Working example: http://rubular.com/r/TuADb2vB4w

Note that the problem becomes very simple if you can solve it in two steps: filter out lines with \bbears\b, and match the wanted string.

不知所踪 2024-11-16 10:14:11

在 Perl 中:

use strict;
use warnings;

my $a = "Some bears live and eat in the woods.";

#$a = "Some animals live and eat in the woods, like bears.";

$a = "Some animals live and eat bears in the woods.";

$a =~ /(?(?!.*bears.*)(.*\blive and\b(.*)\bwoods\b.*)|(.{0}))/g;

print $2;

条件 ?(?!.*bears.*) 就像

if the string contains the bears string
   then matches .*\blive and\b(.*)\bwoods\b.*
   else matches .{0}

您不需要环视来匹配 live 和woods

此处阅读有关正则表达式条件的更多信息

In Perl:

use strict;
use warnings;

my $a = "Some bears live and eat in the woods.";

#$a = "Some animals live and eat in the woods, like bears.";

$a = "Some animals live and eat bears in the woods.";

$a =~ /(?(?!.*bears.*)(.*\blive and\b(.*)\bwoods\b.*)|(.{0}))/g;

print $2;

The condition ?(?!.*bears.*) is like

if the string contains the bears string
   then matches .*\blive and\b(.*)\bwoods\b.*
   else matches .{0}

Youd don't need lookarounds to match between live and and woods

Read more about Regex Conditionals here

相对绾红妆 2024-11-16 10:14:11

您的问题和示例似乎不匹配,但您可以执行以下操作:(

(?!.*\bbears\b)(?<=\blive and\b).*?(?=\bwoods\b)

只是扩展了您的正则表达式)

Your question and the examples don't seem to match, but you can do something like:

(?!.*\bbears\b)(?<=\blive and\b).*?(?=\bwoods\b)

(just expanded your regex)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文