正则表达式前瞻 n 次 python
regex lookahead n times python
我以下面这句话为例
isaac morka morka morka
我正在尝试获得以下结果:
isaac morka
我尝试了以下代码:
re.findall(r'isaac[\s\w]+(?=morka)', 'isaac morka morka morka')
但得到的结果不正确
['isaac morka morka']
您可以在不使用环视的情况下简化您的正则表达式 isaac[\s\S]+?morka
。
在此处测试正则表达式:https://regex101.com/r/bmp5pH/1
您可以使用
rgx = r'\bisaac\b|\bmorka\b(?!.*\bmorka\b)'
str = 'isaac morka morka morka'
re.findall(rgx, str)
#=> ["isaac", "morka"]
Python demo<-\(ツ)/->Regex demo
我们来分解正则表达式。
\bisaac\b # match 'isaac' with word boundaries fore and aft
| # or
\bmorka\b # match 'morca' with word boundaries fore and aft
(?! # begin negative lookahead
.* # match zero or more characters
\bmorka\b # match 'morca' with word boundaries fore and aft
) # end negative lookahead
如果要返回一个唯一单词列表,使得列表中的每个单词在字符串中至少出现一次,则可以编写以下内容。
str = "isaac morka louie morka isaac morka"
rgx = r'\b(\w+)\b(?!.*\b\b)'
re.findall(rgx, str)
#=> ['louie', 'isaac', 'morka']
\b(\w+)\b # match one or more word characters with word boundaries
# fore and aft and save to capture group 1
(?! # begin negative lookahead
.* # match zero or more characters
\b\b # match the content of capture group 1 with word boundaries
# fore and aft
) # end negative lookahead
正则表达式与@anotherGatsby 使用的相同。下面的代码片段给出了您需要的结果。
x=['isaac morka morka']
str = str(x)
rex =re.compile('isaac[\s\S]+?morka')
print(re.findall(rex,str))
我以下面这句话为例
isaac morka morka morka
我正在尝试获得以下结果:
isaac morka
我尝试了以下代码:
re.findall(r'isaac[\s\w]+(?=morka)', 'isaac morka morka morka')
但得到的结果不正确
['isaac morka morka']
您可以在不使用环视的情况下简化您的正则表达式 isaac[\s\S]+?morka
。
在此处测试正则表达式:https://regex101.com/r/bmp5pH/1
您可以使用
rgx = r'\bisaac\b|\bmorka\b(?!.*\bmorka\b)'
str = 'isaac morka morka morka'
re.findall(rgx, str)
#=> ["isaac", "morka"]
Python demo<-\(ツ)/->Regex demo
我们来分解正则表达式。
\bisaac\b # match 'isaac' with word boundaries fore and aft
| # or
\bmorka\b # match 'morca' with word boundaries fore and aft
(?! # begin negative lookahead
.* # match zero or more characters
\bmorka\b # match 'morca' with word boundaries fore and aft
) # end negative lookahead
如果要返回一个唯一单词列表,使得列表中的每个单词在字符串中至少出现一次,则可以编写以下内容。
str = "isaac morka louie morka isaac morka"
rgx = r'\b(\w+)\b(?!.*\b\b)'
re.findall(rgx, str)
#=> ['louie', 'isaac', 'morka']
\b(\w+)\b # match one or more word characters with word boundaries
# fore and aft and save to capture group 1
(?! # begin negative lookahead
.* # match zero or more characters
\b\b # match the content of capture group 1 with word boundaries
# fore and aft
) # end negative lookahead
正则表达式与@anotherGatsby 使用的相同。下面的代码片段给出了您需要的结果。
x=['isaac morka morka']
str = str(x)
rex =re.compile('isaac[\s\S]+?morka')
print(re.findall(rex,str))