![](/img/trans.png)
[英]How do you write a regex in python that finds all word which contain only letters, numbers and underscore?
[英]How do I write a regex function in Python that checks if a user has only letters and numbers?
我只希望我的用戶名包含字母,數字和下划線。 沒有其他符號,空格或其他任何符號。
如何編寫正則表達式來檢查它是否只有字母/數字/下划線?
基本上:
import re
regex = re.compile("^[a-zA-Z0-9_]+$")
if regex.match(some_string):
do_something()
>>> re.match('^\w+$', '4tg25g_3yg')
<_sre.SRE_Match object at 0x7f8093f198b8>
"^[a-zA-Z0-9_]+$"
要么
"^[\w_]+$"
這樣的事情應該工作
import re
if re.match("^[A-Za-z0-9_]*$", user_string):
# do something here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.