当前位置:网站首页>[regular expression] matches multiple characters
[regular expression] matches multiple characters
2022-07-27 15:47:00 【Grapefruit tree CC】
1. Match multiple characters
| Code | function |
|---|---|
| * | Match previous character appears 0 Times or infinite times , You can have it or not |
| + | Match previous character appears 1 Times or infinite times , At least 1 Time |
| ? | Match previous character appears 1 Time or 0 Time , That is to say, there is either 1 Time , Or not |
| {m} | Match previous character appears m Time |
| {m,n} | Match previous character appears from m To n Time |
Example 1:*
demand : Match a string, the first letter is the size character , It's followed by lowercase letters and these lowercase letters can Yes, No
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())
Running results :
M
Mnn
Aabcdef
Example 2:+
demand : Match a string , The first character is t, The last string is o, At least one character in the middle
import re
match_obj = re.match("t.+o", "two")
if match_obj:
print(match_obj.group())
else:
print(" Matching failure ")
Running results :
two
Example 3:?
demand : Match such data , however https This s There may be , It could be http This s No,
import re
match_obj = re.match("https?", "http")
if match_obj:
print(match_obj.group())
else:
print(" Matching failure ")
Running results :
https
Example 4:{m}、{m,n}
demand : Match out ,8 To 20 Bit code , It can be in upper and lower case 、 Numbers 、 Underline
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())
Running results :
12a3g4
1ad12f23s34455ff66
边栏推荐
猜你喜欢
随机推荐
js使用一元运算符简化字符串转数字
Binder initialization process
The shell script reads the redis command in the text and inserts redis in batches
STL value string learning
【剑指offer】面试题39:数组中出现次数超过一半的数字
Spark RPC
面试重点——传输层的TCP协议
UDP 的报文结构和注意事项
表格插入行内公式后,单元格失去焦点
传美国政府将向部分美企发放对华为销售许可证!
聊聊ThreadLocal
【剑指offer】面试题54:二叉搜索树的第k大节点
Insert sort directly
js操作dom节点
Troubleshooting the slow startup of spark local programs
[正则表达式] 匹配多个字符
Binder初始化过程
Causes and solutions of deadlock in threads
线程中死锁的成因及解决方案
使用Lombok导致打印的tostring中缺少父类的属性









