正则表达式(regular expression)
\d # 数字:[0-9]
\D # 非数字:[^\d]
\s # 空白字符:[<空格>\t\r\n\f\v]
\S # 非空白字符:[^ls]
\w # 单词字符:[A-Za-z0-9_]
\W # 非单词字符:[^\w]
. # 匹配任意字符
* # 匹配前一个字符0或无限次
+ # 匹配前一个字符1次或无限次
? # 匹配前一个字符0次或1次
{n} # 匹配前一个字符n次
{n,m} # 匹配前一个字符n至m次
^ # 匹配字符串开头,在多行模式中匹配每一行的开头
$ # 匹配字符串末尾,在多行模式中匹配每一行的末尾
\ # 转义字符
| # 匹配左右表达式任意一个,先匹配左边,匹配成功后跳过右边
() # 被括起来的表达式作为分组
[] # 字符集
import re
# re.match(pattern, string, flags=0)
pattern = re.compile(r'hello')
match = pattern.match('hello world!')
# re.search(pattern, string, flags=0)
match = pattern.search('hello world!')
# re.split(pattern, string, maxsplit=0, flags=0)
print(re.split(r'\s+', 'abc 123 abc'))
# re.findall(pattern, string, flags=0)
print(re.findall(r'\d+','abc1abc2abc3'))
# re.finditer(pattern, string, flags=0)
for i in re.finditer(r'\d+','abc1abc2abc3'):
print(i.group())
# re.sub(pattern, repl, string, count=0, flags=0)
print(re.sub(r'\d+', '-', 'abc1abc2abc3'))
贪婪匹配
正则匹配默认是贪婪匹配,也就是匹配尽可能多的字符
加个?
就可以让\d+
采用非贪婪匹配
[^\s] # 匹配任何非空字符
# IP地址
^(((2[0-4]\d)|(25[0-5])|([01]?\d\d?))\.){3}((2[0-4]\d)|(25[0-5])|([01]?\d\d?))$
# email邮箱
^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$
原文:https://www.cnblogs.com/rustling/p/11901931.html