我需要使用 Python 解析字符串并提取由 :
(冒号)分隔的 2 个标记,这些标记可以用单引号、双引号或不带引号括起来。
工作示例:
# <input string> -> <tuple that should return>
1) abc:def -> (abc, def)
2) abc:"def" -> (abc, def)
3) "abc":def -> (abc, def)
4) "abc":"def" -> (abc, def)
5) "a:bc":abc -> (a:bc, abc)
示例案例不起作用:
# <input string> -> <tuple that should return>
6) abc:"a:bc" -> (abc, a:bc)
7) "abcdef" -> (abcdef,)
使用的正则表达式是:
>>> import re
>>> rex = re.compile(r"(?P<fquote>[\'\"]?)"
r"(?P<user>.+)"
r"(?P=fquote)"
r"(?:\:"
r"(?P<squote>[\'\"]?)"
r"(?P<pass>.+)"
r"(?P=squote))")
我有两个问题,首先是示例案例 6) 和 7) 不起作用,第二个是在 rex.match
之后,我希望所有组都匹配,但不希望 fquote
和 squote
的。我的意思是现在 rex.match("'abc':'def').groups()
返回 ("'", "abc", "'", "def")
,我只想要 ("abc", "def")
。
有什么想法吗?
谢谢
最佳答案
您可以在此处使用 csv
模块而不是正则表达式:
inputs = [
'abc:def', 'abc:"def"', '"abc":def', '"abc":"def"', '"a:bc":abc', #working
'abc:"a:bc"', 'abcdef' # not working
]
import csv
for idx, el in enumerate(inputs, start=1):
print idx, tuple(next(csv.reader([el], delimiter=':')))
这给你:
1 ('abc', 'def')
2 ('abc', 'def')
3 ('abc', 'def')
4 ('abc', 'def')
5 ('a:bc', 'abc')
6 ('abc', 'a:bc')
7 ('abcdef',)
关于python - 正则表达式贪心问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15343228/