[英]REGEX_String between strings in a list
From this list:从这个列表中:
['AUSTRALIA\nBELMONT PARK (WA', '\nR3\n1/5/4/2\n2/3/1/5\nEAGLE FARM (QLD']
I would like to reduce it to this list:我想把它减少到这个列表:
['BELMONT PARK', 'EAGLE FARM']
You can see from the first list that the desired words are between '\n'
and '('
.您可以从第一个列表中看到所需的单词介于
'\n'
和'('
之间。
My attempted solution is:我尝试的解决方案是:
for i in x:
result = re.search('\n(.*)(', i)
print(result.group(1))
This returns the error 'unterminated subpattern'.这将返回错误“未终止的子模式”。 Thankyou
谢谢
You're getting an error because the (
is unescaped. Regardless, it will not work, as you'll get the following matches:您收到错误是因为
(
未转义。无论如何,它将无法正常工作,因为您将获得以下匹配项:
\nBELMONT PARK (
\nR3\n1/5/4/2\n2/3/1/5\nEAGLE FARM (
You can try the following:您可以尝试以下方法:
(?<=\\n)(?!.*\\n)(.*)(?= \()
(?<=\\n)
: Positive lookbehind to ensure \n
is before match (?<=\\n)
: 正向向后看以确保\n
在匹配之前(?!.*\\n)
: Negative lookahead to ensure no further \n
is included (?!.*\\n)
:负前瞻以确保不包含进一步的\n
(.*)
: Your match (.*)
: 你的比赛(?= \()
: Positive lookahead to ensure (
is after match (?= \()
:正向前瞻以确保(
是在匹配之后You can get the matches without using any lookarounds, as you are already using a capture group.您可以在不使用任何环视的情况下获得匹配项,因为您已经在使用捕获组。
\n(.*) \(
Explanation解释
\n
Match a newline \n
匹配换行符(.*)
Capture group 1, match any character except a newline, as much as possible (.*)
捕获组1,尽可能匹配除换行符以外的任何字符\(
Match a space and (
\(
匹配一个空格和(
See a regex101 demo and a Python demo .请参阅regex101 演示和Python 演示。
Example例子
import re
x = ['AUSTRALIA\nBELMONT PARK (WA', '\nR3\n1/5/4/2\n2/3/1/5\nEAGLE FARM (QLD']
pattern = r"\n(.*) \("
for i in x:
m = re.search(pattern, i)
if m:
print(m.group(1))
Output输出
BELMONT PARK
EAGLE FARM
If you want to return a list:如果要返回列表:
x = ['AUSTRALIA\nBELMONT PARK (WA', '\nR3\n1/5/4/2\n2/3/1/5\nEAGLE FARM (QLD']
pattern = r"\n(.*) \("
res = [m.group(1) for i in x for m in [re.search(pattern, i)] if m]
print(res)
Output输出
['BELMONT PARK', 'EAGLE FARM']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.