python 中re模块

编程入门 行业动态 更新时间:2024-10-07 12:28:10

python 中re<a href=https://www.elefans.com/category/jswz/34/1771428.html style=模块"/>

python 中re模块

re

python中re模块提供了正则表达式相关操作
字符:

.   匹配除换行符以外的任意字符  加匹配模式 re.S 后.可以匹配任意字符
\w  匹配字母或数字或下划线或汉字
\s  匹配任意的空白符
\d  匹配数字
\b  匹配单词的开始或结束
^   匹配字符串的开始
$   匹配字符串的结束

次数:

*    重复零次或更多次
+    重复一次或更多次
?    重复零次或一次
{n}  重复n次
{n,}  重复n次或更多次
{n,m}  重复n到m次

分组

1.使用()给正则分组,作为一个整体。2.给正则分组取名(?P<id_name>\d{3})

1.re的findall

# findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;
# 空的匹配也会包含在结果中
#findall(pattern, string, flags=0)# 无分组r = re.findall("a\w+",origin)print(r)
# 有分组origin = "hello alex bcd abcd lge acd 19"r = re.findall("a((\w*)c)(d)", origin)print(r)

2.re的search

# search,浏览整个字符串去匹配第一个,未匹配成功返回None
# search(pattern, string, flags=0)
# 无分组r = re.search("a\w+", origin)print(r.group())     # 获取匹配到的所有结果print(r.groups())    # 获取模型中匹配到的分组结果print(r.groupdict()) # 获取模型中匹配到的分组结果# 有分组r = re.search("a(\w+).*(?P<name>\d)$", origin)print(r.group())     # 获取匹配到的所有结果print(r.groups())    # 获取模型中匹配到的分组结果print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组

3.re的match

match,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None###使用格式match(pattern, string, flags=0)# pattern: 正则模型# string : 要匹配的字符串# falgs  : 匹配模式X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.I  IGNORECASE  Perform case-insensitive matching.M  MULTILINE   "^" matches the beginning of lines (after a newline)as well as the string."$" matches the end of lines (before a newline) as wellas the end of the string.S  DOTALL      "." matches any character at all, including the newline.A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \Dmatch the corresponding ASCII character categories(rather than the whole Unicode categories, which is thedefault).For bytes patterns, this flag is the only availablebehaviour and needn't be specified.L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.U  UNICODE     For compatibility only. Ignored for string patterns (itis the default), and forbidden for bytes patterns.# 无分组r = re.match("h\w+", origin)print(r.group())     # 获取匹配到的所有结果print(r.groups())    # 获取模型中匹配到的分组结果print(r.groupdict()) # 获取模型中匹配到的分组结果# 有分组
# 为何要有分组?提取匹配成功的指定内容(先匹配成功全部正则,再匹配成功的局部内容提取出来)r = re.match("h(\w+).*(?P<name>\d)$", origin)print(r.group())     # 获取匹配到的所有结果print(r.groups())    # 获取模型中匹配到的分组结果print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组

4.sub

# sub,替换匹配成功的指定位置字符串sub(pattern, repl, string, count=0, flags=0)
# pattern: 正则模型
# repl   : 要替换的字符串或可执行对象
# string : 要匹配的字符串
# count  : 指定匹配个数
# flags  : 匹配模式# 与分组无关origin = "hello alex bcd alex lge alex acd 19"r = re.sub("a\w+", "999", origin, 2)print(r)
# 有分组origin = "hello alex bcd alex lge alex acd 19"r = re.sub("(a\w+)", r"\1 999", origin, 2)    \1 表示取第一个元组内容替换print(r)

5.split

# split,根据正则匹配分割字符串split(pattern, string, maxsplit=0, flags=0)
# pattern: 正则模型
# string : 要匹配的字符串
# maxsplit:指定分割个数
# flags  : 匹配模式# 无分组origin = "hello alex bcd alex lge alex acd 19"r = re.split("alex", origin, 1)print(r)# 有分组origin = "hello alex bcd alex lge alex acd 19"r1 = re.split("(alex)", origin, 1)print(r1)r2 = re.split("(al(ex))", origin, 1)print(r2)

6.常用正则表达式

IP地址:
^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$手机号:
^1[3|4|5|8][0-9]\d{8}$邮箱:
[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+

更多推荐

python 中re模块

本文发布于:2024-03-15 09:57:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1738632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!