2013-07-06 51 views
0

請你能不能幫我一下:正則表達式匹配之間的「東西」

考慮以下行:

"Duplicate entry '[email protected]' for key 'email'" 

我需要匹配之間的任何事物的正則表達式'incloding空格,新線,並且還

使用的代碼IM「任意 '之間的字符' 是:(Python)的

value = "Duplicate entry '[email protected]' for key 'email'" 
dict = re.findall(r'regex goes here', value) 
print dict[0] 
print dict[1] 

我需要打印:

[email protected] 

email 
+1

它是如何應該告訴一個「'」下一個? –

+0

我不知道,它似乎是不可能的: –

+0

爲什麼你不給一個*真實*的消息的例子,我們會告訴你你應該如何解析它 –

回答

2
>>> m = re.match("Duplicate entry '(?P<value>.*)' for key '(?P<key>.*)'", "Duplicate entry '[email protected]' for key 'email'") 
>>> m.group('value') 
'[email protected]' 
>>> m.group('key') 
'email' 
1
string = "Duplicate entry '[email protected]' for key 'email'" 
pattern = re.compile("'[^']*'") 
matches = pattern.findall(string) 
# matches == ["'[email protected]'", "'email'"] 

如果要強制引號中都有至少一個字符:從

pattern = re.compile("'[^']+') 
相關問題