2014-11-21 37 views
1

我正在嘗試編寫一個python腳本的登錄例程。在這樣做的時候,我發現需要在整個詞的基礎上模式匹配憑證。我曾試圖RegEx這個,但由於我不清楚的原因,這是失敗的,但我希望這裏的人很明顯。代碼和輸出:Python RegEx與字邊界

import re 

authentry = "testusertestpass" 
username = "testuser" 
password = "testpass" 
combo = "r\'\\b"+username + password + "\\b\'" 
testcred = re.search(combo, authentry) 
print combo 
print authentry 
print testcred 

r'\btestusertestpass\b' 
testusertestpass 
None 

,會出現我正則表達式的測試,至少對我來說,要正確格式化,而應該是對測試字符串直接匹配,但並非如此。有任何想法嗎?非常感謝任何見解!

+1

'R '\ B' +用戶名+密碼+ R'\ b'''r'前一個字符串文字意味着原始字符串的文本,這具有稍微不同的解析規則。 – nhahtdh 2014-11-21 08:43:52

回答

-1

試試這個:它可能有效。

import re 

authentry = "testusertestpass with another text" 
username = "testuser" 
password = "testpass" 
combo = username + password + r'\b' 
testcred = re.search(combo, authentry) 
print combo 
print authentry 
print testcred 

輸出:

testusertestpass\b 
testusertestpass with another text 
<_sre.SRE_Match object at 0x1b8a030> 
+0

感謝您的評論!雖然這確實會給出一個匹配,但它也匹配字符串「testusertestpass」的任何子集,例如「testusertestpas」 – shanesmith30286 2014-11-21 10:51:54

+0

然後,只能使用「用戶名+密碼」。用輸入和輸出清楚地告訴我 – 2014-11-21 11:00:54