当前位置:网站首页>正则表达式:示例(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'>
边栏推荐
- ThreeDPoseTracker项目解析
- Unity | two ways to realize facial drive
- Threedposetracker project resolution
- Opinions on softmax function
- ORA-00030
- VMware Tools installation error: unable to automatically install vsock driver
- Paddle framework: paddlenlp overview [propeller natural language processing development library]
- 竞赛题 2022-6-26
- dried food! Accelerating sparse neural network through hardware and software co design
- MATLB|实时机会约束决策及其在电力系统中的应用
猜你喜欢
【详细】快速实现对象映射的几种方式
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
VMware Tools installation error: unable to automatically install vsock driver
MUX VLAN configuration
现货白银的一般操作方法
Leetcode skimming questions_ Verify palindrome string II
A picture to understand! Why did the school teach you coding but still not
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Cookie concept, basic use, principle, details and Chinese transmission
Unity | two ways to realize facial drive
随机推荐
Force buckle 1020 Number of enclaves
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
3D模型格式汇总
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
[flask] official tutorial -part1: project layout, application settings, definition and database access
Basic operations of database and table ----- set the fields of the table to be automatically added
VMware Tools installation error: unable to automatically install vsock driver
LeetCode 322. Change exchange (dynamic planning)
Loop structure of program (for loop)
Win10 add file extension
MATLB|实时机会约束决策及其在电力系统中的应用
Code Review关注点
基於DVWA的文件上傳漏洞測試
Redis' cache penetration, cache breakdown, cache avalanche
Idea sets the default line break for global newly created files
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell
Leetcode1961. Check whether the string is an array prefix
Who knows how to modify the data type accuracy of the columns in the database table of Damon
[flask] static file and template rendering