使用正则表达式分割多个字符串

时间:2018-07-30 23:59:10

标签: python regex string

[Delta-1234, United-1345] Testing different airlines
[Delta-1234] Testing different airlines

我想在第一种情况下获得Delta-1234和United-1345,在第二种情况下获得Delta-1234。可以使用findall吗?

4 个答案:

答案 0 :(得分:1)

您真的需要正则表达式吗?您可以在括号[]

之间找到元素
x = lambda s: s[s.index('['):s.index("]")+1]

string1 = "[Delta-1234, United-1345] Testing different airlines"
string2 = "[Delta-1234] Testing different airlines"

print(x(string1))
print(x(string2))

输出

[Delta-1234, United-1345]
[Delta-1234]

答案 1 :(得分:0)

如果要使用正则表达式,只需匹配[,然后(贪婪地)捕获重复的非]

>>> regex = re.compile(r"\[([^\]]+)")
>>> re.findall(regex, "[Delta-1234, United-1345] Testing different airlines")
['Delta-1234, United-1345']
>>> re.findall(regex, "[Delta-1234] Testing different airlines")
['Delta-1234']

或使用向后看

>>> regex = re.compile(r"(?<=\[)[^\]]+")
>>> re.findall(regex, "[Delta-1234, United-1345] Testing different airlines")
['Delta-1234, United-1345']
>>> re.findall(regex, "[Delta-1234] Testing different airlines")
['Delta-1234']

答案 2 :(得分:0)

使用正则表达式实现此目的的另一种方法是:

import re

str1 = "[Delta-1234, United-1345] Testing different airlines"
str2 = "[Delta-1234] Testing different airlines"

regex_pattern = r"[^[]*\[([^]]*)\]"

print(re.match(regex_pattern, str1).groups()[0])
print(re.match(regex_pattern, str2).groups()[0])

它将打印

Delta-1234, United-1345
Delta-1234

答案 3 :(得分:0)

给出:

s='''\
[Delta-1234, United-1345] Testing different airlines
[Delta-1234] Testing different airlines'''

您可以这样做:

>>> [e.split(', ') for e in re.findall(r'\[([^]]+)\]', s)]
[['Delta-1234', 'United-1345'], ['Delta-1234']]