带单词边界的Python正则表达式

带单词边界的Python正则表达式,python,regex,login,Python,Regex,Login,我正在尝试为python脚本编写一个登录例程。在这样做的过程中,我发现需要在整个单词的基础上对凭证进行模式匹配。我曾尝试将其正则化,但由于我不清楚的原因,它失败了,但我希望这里的人能明白这一点。代码和输出: import re authentry = "testusertestpass" username = "testuser" password = "testpass" combo = "r\'\\b"+username + password + "\\b\'" testcred = re

我正在尝试为python脚本编写一个登录例程。在这样做的过程中,我发现需要在整个单词的基础上对凭证进行模式匹配。我曾尝试将其正则化,但由于我不清楚的原因,它失败了,但我希望这里的人能明白这一点。代码和输出:

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

因此,我的正则表达式测试看起来(至少对我来说)格式正确,应该与测试字符串直接匹配,但不是。有什么想法吗?非常感谢您的洞察力

试试这个:它可能有用

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>
testusertestpass\b
testusertestpass和另一个文本

r'\b'+用户名+密码+r'\b'
r
之前的字符串文字表示原始字符串文字,其解析规则略有不同。感谢您的评论!虽然这确实提供了一个匹配,但它也匹配字符串“testusertestpass”的任何子集,例如“testusertestpass”,然后只使用
username+password
。输入和输出对我来说都很清楚