2014-11-21 56 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