简体   繁体   English

将正则表达式与可选的前瞻匹配

[英]match a regular expression with optional lookahead

I have the following strings: 我有以下字符串:

NAME John Nash FROM California

NAME John Nash

I want a regular expression capable of extracting 'John Nash' for both strings. 我想要一个能够为两个字符串提取“John Nash”的正则表达式。

Here is what I tried 这是我试过的

"NAME(.*)(?:FROM)"
"NAME(.*)(?:FROM)?"
"NAME(.*?)(?:FROM)?"

but none of these works for both strings. 但这两个字符串都不起作用。

You can use logical OR between FROM and anchor $ : 您可以在FROM和anchor $之间使用逻辑OR:

NAME(.*)(?:FROM|$)

See demo https://regex101.com/r/rR3gA0/1 请参阅演示https://regex101.com/r/rR3gA0/1

In this case after the name it will match FROM or the end of the string.But in your regex since you make the FROM optional in firs case it will match the rest of string after the name. 在这种情况下,它将匹配FROM或字符串的结尾。但是在正则表达式中,因为你在第一种情况下使用FROM可选项,它将匹配名称后面的其余字符串。

If you want to use a more general regex you better to create your regex based on your name possibility shapes for example if you are sure that your names are create from 2 word you can use following regex : 如果您想使用更通用的正则表达式,最好根据您的名称可能性形状创建正则表达式,例如,如果您确定您的名称是从2个单词创建的,则可以使用以下正则表达式:

NAME\s(\w+\s\w+)

Demo https://regex101.com/r/kV2eB9/2 演示https://regex101.com/r/kV2eB9/2

Make the second part of the string optional (?: FROM.*?)? 使字符串的第二部分可选(?: FROM.*?)? , ie: ,即:

NAME (.*?)(?: FROM.*?)?$

MATCH 1
1.  [5-14]  `John Nash`
MATCH 2
1.  [37-46] `John Nash`
MATCH 3
1.  [53-66] `John Doe Nash`

Regex Demo 正则表达式演示
https://regex101.com/r/bL7kI2/2 https://regex101.com/r/bL7kI2/2

You can do without regex: 你可以没有正则表达式:

>>> myStr = "NAME John Nash FROM California"
>>> myStr.split("FROM")[0].replace("NAME","").strip()
'John Nash'
 r'^\w+\s+(\w+\s+\w+) - word at start of string
 follows by one or more spaces and
 two words and at least one space between them

with open('data', 'r') as f:
    for line in f:
      mo =   re.search(r'^\w+\s+(\w+\s+\w+)',line)
      if mo:
        print(mo.group(1))

John Nash
John Nash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM