繁体   English   中英

带有单词边界的Python RegEx

[英]Python RegEx with word boundaries

我正在尝试为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

因此,至少在我看来,我的正则表达式测试似乎已正确格式化,并且应该与测试字符串直接匹配,但事实并非如此。 有任何想法吗? 非常感谢您的见解!

试试这个:可能有效。

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM