当前位置:网站首页>正则表达式:示例(1)
正则表达式:示例(1)
2022-07-06 01:33:00 【不负韶华ღ】
1、用户名
用户名可以包含以下几种字符:
1、26 个大小写英文字母表示为 a-zA-Z。
2、数字表示为 0-9。
3、下划线表示为 _。
4、中划线表示为 -。
用户名由若干个字母、数字、下划线和中划线组成,所以需要用到 + 表示 1 次或多次出现。根据以上条件得出用户名的表达式可以为:
[a-zA-Z0-9-_]+
>>> import re
>>> pattern = r"[a-zA-Z0-9_-]+"
>>> text = "lily123"
>>> match = re.search(pattern, text)
>>> match
<re.Match object; span=(0, 7), match='lily123'>
2、HTML标签
匹配html中的<p>标签:
<p((\s\S)*)<\/p>
>>> import re
>>> text = '''<p class="odd" id="odd">123</p>'''
>>> pattern = r"<p(([\s\S])*)<\/p>"
>>> re.search(pattern, text)
<re.Match object; span=(0, 31), match='<p class="odd" id="odd">123</p>'>
3、手机号
手机号第一位为1,第二位不能为0、1、2,一共有11位。根据以上条件得出的正则表达式为:
^1[^012]\d{9}$
>>> import re
>>> pattern = r"^1[^012]\d{9}$"
>>> phone_number = "13174335288"
>>> match = re.search(pattern, phone_number)
>>> match
<re.Match object; span=(0, 11), match='13174335288'>
边栏推荐
猜你喜欢
037 PHP login, registration, message, personal Center Design
Basic operations of databases and tables ----- primary key constraints
Docker compose配置MySQL并实现远程连接
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
[detailed] several ways to quickly realize object mapping
Opinions on softmax function
Poj2315 football games
Basic operations of databases and tables ----- unique constraints
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
Yii console method call, Yii console scheduled task
随机推荐
国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
MATLB|实时机会约束决策及其在电力系统中的应用
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
Force buckle 9 palindromes
Code Review关注点
Three methods of script about login and cookies
WGet: command line download tool
DOM introduction
【全網最全】 |MySQL EXPLAIN 完全解讀
Basic operations of databases and tables ----- default constraints
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
MATLB | real time opportunity constrained decision making and its application in power system
电气数据|IEEE118(含风能太阳能)
Nmap: network detection tool and security / port scanner
A Cooperative Approach to Particle Swarm Optimization
XSS learning XSS lab problem solution
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
Basic process and testing idea of interface automation