Python3,与列表重新匹配 [英] Python3, re.match with list
本文介绍了Python3,与列表重新匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我读了这篇文章: https://stackoverflow.com/a/17090205/6426449
然后我列出了无法在Django中的用户名上使用的列表.
And I made a list that cannot be used on username in django.
列表: FORBIDDEN_USERNAME_LIST = ['admin','master','owner']
所以我做了这样的代码:
So I made a code like this :
views.py
def username_choice(request):
if request.method == "POST":
username = request.POST['username']
for item in forbidden.FORBIDDEN_USERNAME_LIST:
match = re.search("r'\b"+item+"\b'", username)
if match:
return JsonResponse({'result': item + 'banned username'})
但是似乎不起作用.
也许我认为, match = re.search("r'\ b" + item +"\ b'",用户名)
这是问题.
Maybe I think, match = re.search("r'\b"+item+"\b'", username)
Here is problem.
我该如何解决?
推荐答案
您可以简单地在中使用
:
forbidden = ['admin', 'master', 'owner']
username = request.POST['username']
match = [nm for nm in forbidden if nm in username]
if match:
# part of the username is in the forbidden list.
IPython中的示例:
Example in IPython:
In [1]: forbidden = ['admin', 'master', 'owner']
In [2]: username = 'owner123'
In [3]: match = [nm for nm in forbidden if nm in username]
In [4]: match
Out[4]: ['owner']
这篇关于Python3,与列表重新匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文