如何在正则表达式中匹配多个匹配项中的正向先行断言后面的匹配项?

huangapple go评论54阅读模式
英文:

How to match the match just behind a positive lookahead when there are multiple matches, in regex?

问题

我是新手

我有一个示例文本

abcd

efgh

ijk

DOB

我想要匹配仅在DOB之前出现的行

我尝试过

(?Pa-zA-Z)(?=DOB)

但这不起作用

如何仅匹配ijk?

英文:

I'm new to regex,

I have a sample text

abcd

efgh 

ijk

DOB

I wanted to match only the line occurring just behind DOB

I tried:

(?P<name>a-zA-Z)(?=DOB)

but this doesn't work,

How to match only ijk?

答案1

得分: 1

你可以使用多行正则表达式:

print(re.search('(?m)^([a-zA-Z]+)$\\s*(?=DOB)', s).group(1))
英文:

You can use a multiline regular expression:

print(re.search('(?m)^([a-zA-Z]+)$\s*(?=DOB)', s).group(1))

答案2

得分: 1

你可以匹配正则表达式

(?mi)^[a-z]+(?=(?:\r?\n)+DOB\r?\n)

演示

该表达式包含以下元素。

(?mi)         # 使用标志m和i匹配余下部分
^             # 匹配行的开头
[a-z]+        # 匹配一个或多个字母
(?=           # 开始正向预查
  (?:\r?\n)+  # 匹配一个或多个行终止符
  DOB\r?\n    # 匹配 'DOB' 后跟一个行终止符
)             # 结束正向预查

多行标志 m(在 (?mi) 中)导致 ^ 匹配行的开头和结尾。不区分大小写标志 i 导致 [a-z] 匹配任何小写或大写字母。如果文件是在使用Windows的计算机上创建的,则需要 \r\r? 使它变为可选。

英文:

You can match the regular expression

(?mi)^[a-z]+(?=(?:\r?\n)+DOB\r?\n)

Demo

The expression has the following elements.

(?mi)         # match the remainder with the flags m and i
^             # match the beginning of the line
[a-z]+        # match one or more letters
(?=           # begin a positive lookahead
  (?:\r?\n)+  # match one or more line terminators
  DOB\r?\n    # match 'DOB' followed by a line terminator
)             # end the positive lookahead

The multiline flag m (in (?mi)) causes ^ to match the start and end of a line. The case insensitive flag i causes [a-z] to match any lowercase or uppercase letter. \r is needed if the file was created on a computer using Windows. \r? makes it optional.

答案3

得分: 0

这是您想要实现的正则表达式模式的部分翻译:

[^\n]+(?=\nDOB)

在此处验证

解释:

  • [^\n]+:一个否定字符类,带有一个量词,表示匹配除了换行符之外的任何字符,一个或多个
  • (?=\nDOB):前瞻,断言字符串后面跟着一个换行符和文字“DOB”

这样,您可以获取最后一行之前的字符串。

英文:

Here's the regex pattern for what you want to achieve:

[^\n]+(?=\nDOB)

Verify here

Explanation:

  • [^\n]+: a negated character class with a quantifier which says match ONE or MORE of any characters except a new line
  • (?=\nDOB): lookahead which asserts that the string is followed by a new line and literal DOB

This way you get the string, before the last line.

huangapple
  • 本文由 发表于 2023年2月24日 14:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定