首页 > 编程语言 > 详细

Python_正则表达式

时间:2019-11-21 00:10:06      阅读:106      评论:0      收藏:0      [点我收藏+]

1. 正则表达式

正则表达式(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次

^  # 匹配字符串开头,在多行模式中匹配每一行的开头
$  # 匹配字符串末尾,在多行模式中匹配每一行的末尾

\  # 转义字符
|  # 匹配左右表达式任意一个,先匹配左边,匹配成功后跳过右边
() # 被括起来的表达式作为分组
[] # 字符集

2. re模块

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'))

3. 注意事项

  1. 贪婪匹配

    正则匹配默认是贪婪匹配,也就是匹配尽可能多的字符

    加个?就可以让\d+采用非贪婪匹配

4. 常用正则表达式

[^\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}$

Python_正则表达式

原文:https://www.cnblogs.com/rustling/p/11901931.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!