如何在正则表达式中组合正负条件?

发布于 2024-08-03 07:40:12 字数 542 浏览 8 评论 0原文

我对正则表达式相当陌生,需要一些帮助。我需要在 Perl 中使用正则表达式过滤一些行。我将把正则表达式传递给另一个函数,因此它需要在一行中完成。

我只想选择包含 "too long" 且不以 "SKIPPING" 开头的行,

这是我的测试字符串:

由于到期时间太长而跳过该债券
TKIPPING 该债券到期时间过长
由于到期时间过长而对该债券进行拍打
你好,这个成熟期太长了
这太长了
你好

表达式规则应与“太长”匹配以下内容:

由于到期时间过长而跳过该债券
由于到期时间过长而对该债券进行拍打
你好,这个成熟期太长了
这太长了

,应该跳过:

“你好”,因为它不包含“太长”
“由于到期太久而跳过此债券”,因为它包含“跳过”

I fairly new to regular expressions and need some help. I need to filter some lines using regex in Perl. I am going to pass the regex to another function so it needs to be done in a single line.

I want to select only lines that contain "too long"and that don't begin with "SKIPPING"

Here are my test strings:

SKIPPING this bond since maturity too long
TKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long
hello there

The regex rule should match the following on 'too long":

SKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long

and it should skip:

"hello there" because it doesn't contain 'too long'
"SKIPPING this bond since maturity too long" because it containst 'SKIPPING'

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

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

发布评论

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

评论(6

信仰 2024-08-10 07:40:12
/^(?!SKIPPING).*too long/
/^(?!SKIPPING).*too long/
岁吢 2024-08-10 07:40:12

就我个人而言,我会将其作为两个单独的正则表达式来执行,只是为了使其更清晰。

while (<FILE>)
{
  next if /^SKIPPING/;
  next if !/too long/;

   ... do stuff
}

Personally, I'd do this as two separate regex just to make it clearer.

while (<FILE>)
{
  next if /^SKIPPING/;
  next if !/too long/;

   ... do stuff
}
二智少女 2024-08-10 07:40:12

我怀疑你可能在一个正则表达式之后,但我更喜欢分成更具可读性的东西,如下所示:

use strict;
use warnings;

for my $line ( <DATA> ) {
    next  if $line =~ m/^SKIPPING/;
    next  if $line !~ m/too long/;

    # do something with $line
    chomp $line;
    say "Found: ", $line, ':length=', length( $line );
}

__DATA__
SKIPPING this bond since maturity too long
TKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long
hello there

I suspect you maybe after a single regex however I prefer to split into something more readable like this:

use strict;
use warnings;

for my $line ( <DATA> ) {
    next  if $line =~ m/^SKIPPING/;
    next  if $line !~ m/too long/;

    # do something with $line
    chomp $line;
    say "Found: ", $line, ':length=', length( $line );
}

__DATA__
SKIPPING this bond since maturity too long
TKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long
hello there
囍孤女 2024-08-10 07:40:12

使用前瞻;请参阅正则表达式环视的说明

^(?!SKIPPING).*too long

Use a lookahead; see this explanation of regex lookaround.

^(?!SKIPPING).*too long
山川志 2024-08-10 07:40:12
/^(?<!SKIPPING).*too long$/

匹配您正在寻找的线路。末尾的美元符号导致它仅匹配以“too long”结尾的字符串。

希望这有帮助!

/^(?<!SKIPPING).*too long$/

Matches the lines you're looking for. The dollar sign at the end causes it to match only strings that end with "too long".

Hope this helps!

停滞 2024-08-10 07:40:12

使用负向后查找:

(?<!^SKIPPING)too long$

Using negative lookbehind:

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