简体   繁体   English

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

[英]Splitting multiple strings using regular expression

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

I want to get Delta-1234 and United-1345 in the first case and just Delta-1234 in the second. 我想在第一种情况下获得Delta-1234和United-1345,在第二种情况下仅获得Delta-1234。 Is it possible using findall? 是否可以使用findall?

Do you really need regular expressions? 您真的需要正则表达式吗? You can just find elements between the brackets [ and ] 您可以在方括号[]之间找到元素

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))

outputs 输出

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

If you want to use a regular expression, just match [ , and then (greedily) capture repeated non- ] s: 如果要使用正则表达式,只需匹配[ ,然后(贪婪地)捕获重复的非]

>>> 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']

Or use lookbehind 或使用后向

>>> 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']

Another way to achieve this using regex is: 使用正则表达式实现此目的的另一种方法是:

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])

It will print 它将打印

Delta-1234, United-1345
Delta-1234

Given: 鉴于:

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

You can do: 你可以做:

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

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

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