英文:
How to match the match just behind a positive lookahead when there are multiple matches, in regex?
问题
我是新手
我有一个示例文本
abcd
efgh
ijk
DOB
我想要匹配仅在DOB之前出现的行
我尝试过
(?P
但这不起作用
如何仅匹配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)
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)
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论