简体   繁体   English

如何在Python中编写正则表达式函数来检查用户是否只有字母和数字?

[英]How do I write a regex function in Python that checks if a user has only letters and numbers?

I only want my usernames to have letters, numbers, and underscores. 我只希望我的用户名包含字母,数字和下划线。 No other symbols, spaces, or anything else. 没有其他符号,空格或其他任何符号。

How can I write a regex to check if it's only letters/numbers/underscores? 如何编写正则表达式来检查它是否只有字母/数字/下划线?

Basically: 基本上:

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_]+$"

Something like this should work 这样的事情应该工作

import re
if re.match("^[A-Za-z0-9_]*$", user_string):
    # do something here

暂无
暂无

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

相关问题 您如何在python中编写正则表达式,以查找仅包含字母,数字和下划线的所有单词? - How do you write a regex in python that finds all word which contain only letters, numbers and underscore? 如何编写一个验证器来检查一条记录是否只有一个字段 int=1 ? - How do I write a validator that checks whether a record has only a single field with int=1 among several? 如何创建一个过程来检查用户是否只输入了数字和浮点数? - How do I make a process that checks if the user only entered numbers and floating points? JSON 中的数据有我不需要的字母和数字,如何在 Python 中获取我需要的数据 - Data inside a JSON has letters and numbers I do not need, how to get data I need in Python 如何仅将匹配的正则表达式写入 Python 中的新文件? - How do I write only the matching regex to a new file in Python? 我如何在 python 中编写一个 function 作为输入 2 个数字和 1 个字符串 - How do I write a function in python that takes as input 2 numbers, and 1 string python 中的正则表达式数字和字母 - Regex numbers and letters in python 如何仅使用正则表达式提取数字? - How do I extract numbers only with regex? 如何将数字转换为字母的python列表? - How do I convert a python list which is in numbers into letters? 如何提供错误检查以确保用户输入仅允许字母并在键入数字时提供循环错误消息? - How do I provide error checking to ensure user input only allows letters and provides a looping error message if numbers are typed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM