在Python中需要通过正则表达式对字符串进行匹配的时候,可以使用一个模块,名字为re
//‘match’ scan a string start from the very left,if it not match,it will break
#coding=utf-8
# 导入re模块
import re
# 使用match方法进行匹配操作
result = re.match(正则表达式,要匹配的字符串)
# 如果上一步匹配到数据的话,可以使用group方法来提取数据
result.group()
Match Character
s=r'\w{4,20}@(163|126|qq)\.com'
search
This function will search in the whole string,and find only one result
findall
This function will search all the string satisfy the requirement.
search vs findall
sub
Substitute the string required
1.simple replace
2,Use function to replace the specified string
split
贪婪和非贪婪
Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符;
非贪婪则相反,总是尝试匹配尽可能少的字符。
在”*”,”?”,”+”,”{m,n}”后面加上?,使贪婪变成非贪婪。
原文地址:https://www.jb51.cc/regex/357919.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。