当前位置:网站首页>正则表达式:示例(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'>
边栏推荐
- 【Flask】响应、session与Message Flashing
- 干货!通过软硬件协同设计加速稀疏神经网络
- ctf. Show PHP feature (89~110)
- [Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
- Basic operations of databases and tables ----- default constraints
- 关于softmax函数的见解
- NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
- yii中console方法调用,yii console定时任务
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
- 【详细】快速实现对象映射的几种方式
猜你喜欢

c#网页打开winform exe

How does the crystal oscillator vibrate?

037 PHP login, registration, message, personal Center Design

Unity | two ways to realize facial drive

【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化

Basic operations of database and table ----- delete data table

Docker compose configures MySQL and realizes remote connection

Accelerating spark data access with alluxio in kubernetes

国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!

ORA-00030
随机推荐
XSS learning XSS lab problem solution
A picture to understand! Why did the school teach you coding but still not
Basic operations of databases and tables ----- primary key constraints
Code Review关注点
Kotlin basics 1
Huawei converged VLAN principle and configuration
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
[le plus complet du réseau] | interprétation complète de MySQL explicite
WGet: command line download tool
Initialize MySQL database when docker container starts
UE4 unreal engine, editor basic application, usage skills (IV)
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
安装Redis
VMware Tools安装报错:无法自动安装VSock驱动程序
[flask] obtain request information, redirect and error handling
Accelerating spark data access with alluxio in kubernetes
【Flask】获取请求信息、重定向、错误处理
Unity VR resource flash surface in scene
Leetcode 剑指 Offer 59 - II. 队列的最大值
[flask] official tutorial -part1: project layout, application settings, definition and database access