当前位置:网站首页>[正则表达式] 匹配多个字符
[正则表达式] 匹配多个字符
2022-07-27 14:25:00 【柚子树cc】
1. 匹配多个字符
| 代码 | 功能 |
|---|---|
| * | 匹配前一个字符出现0次或者无限次,即可有可无 |
| + | 匹配前一个字符出现1次或者无限次,即至少有1次 |
| ? | 匹配前一个字符出现1次或者0次,即要么有1次,要么没有 |
| {m} | 匹配前一个字符出现m次 |
| {m,n} | 匹配前一个字符出现从m到n次 |
示例1:*
需求:匹配出一个字符串第一个字母为大小字符,后面都是小写字母并且这些小写字母可 有可无
import re
ret = re.match("[A-Z][a-z]*","M")
print(ret.group())
ret = re.match("[A-Z][a-z]*","MnnM")
print(ret.group())
ret = re.match("[A-Z][a-z]*","Aabcdef")
print(ret.group())
运行结果:
M
Mnn
Aabcdef
示例2:+
需求:匹配一个字符串,第一个字符是t,最后一个字符串是o,中间至少有一个字符
import re
match_obj = re.match("t.+o", "two")
if match_obj:
print(match_obj.group())
else:
print("匹配失败")
运行结果:
two
示例3:?
需求:匹配出这样的数据,但是https 这个s可能有,也可能是http 这个s没有
import re
match_obj = re.match("https?", "http")
if match_obj:
print(match_obj.group())
else:
print("匹配失败")
运行结果:
https
示例4:{m}、{m,n}
需求:匹配出,8到20位的密码,可以是大小写英文字母、数字、下划线
import re
ret = re.match("[a-zA-Z0-9_]{6}","12a3g45678")
print(ret.group())
ret = re.match("[a-zA-Z0-9_]{8,20}","1ad12f23s34455ff66")
print(ret.group())
运行结果:
12a3g4
1ad12f23s34455ff66
边栏推荐
- Network equipment hard core technology insider router chapter Cisco asr9900 disassembly (I)
- Is it safe to open an account on a mobile phone?
- Network equipment hard core technology insider router Chapter 16 dpdk and its prequel (I)
- The first common node of the two linked lists of "Jianzhi offer"
- Spark3中Catalog组件设计和自定义扩展Catalog实现
- Transactions_ Basic demonstrations and transactions_ Default auto submit & manual submit
- Leetcode 74. search two-dimensional matrix bisection /medium
- 修改 Spark 支持远程访问OSS文件
- Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
- 【剑指offer】面试题46:把数字翻译成字符串——动态规划
猜你喜欢

Spark 3.0 DPP实现逻辑

Pictures to be delivered

Summer Challenge harmonyos realizes a hand-painted board

实体类(VO,DO,DTO)的划分

Spark Filter算子在Parquet文件上的下推

With just two modifications, apple gave styleganv2 3D generation capabilities

Leetcode 781. rabbit hash table in forest / mathematical problem medium

Leetcode 783. binary search tree node minimum distance tree /easy

Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`

Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)
随机推荐
学习Parquet文件格式
【剑指offer】面试题51:数组中的逆序对——归并排序
使用解构交换两个变量的值
Several basic uses of tl431-2.5v voltage reference chip
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
Sword finger offer cut rope
Leetcode 240. search two-dimensional matrix II medium
《剑指Offer》 合并两个排序的链表
Network equipment hard core technology insider router Chapter 7 tompkinson roaming the network world (Part 2)
Spark lazy list files 的实现
TCC
Google team launches new transformer to optimize panoramic segmentation scheme CVPR 2022
Four kinds of relay schemes driven by single chip microcomputer
/dev/loop1占用100%问题
Network equipment hard core technology insider router Chapter 13 from deer by device to router (Part 1)
一文读懂鼠标滚轮事件(wheelEvent)
Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
【剑指offer】面试题45:把数组排成最小的数
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)