Python3之re模块
来源:互联网 发布:公务员考试经验知乎 编辑:程序博客网 时间:2024/05/12 10:09
语法
模块内容
match object
match object有布尔值True,可以用来判断是否有匹配到: match = re.search(pattern ,string)
if match:pass
match.group()
match.group()返回匹配到的内容 >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)
'Isaac Newton'
>>> m.group(1)
'Isaac'
>>> m.group(2)
'Newton'
>>> m.group(1, 2)
('Isaac', 'Newton')
re.compile(pattern,flag=0)
把一个正则表达式编译为expression object,然后可以被其他让函数调用
如:
prog = re.compile(pattern)
result = re.match(prog, string)
这样不用每次匹配都编译一遍,节约了时间
re.search(pattern, string, flag=0)
查找正则表达式所匹配的内容,返回一个match对象,如果没有就返回None.
re.match(pattern, string, flag=0)
match与search一样,区别在于match之匹配字符串开头如: re.match("c", "abcdef") # No match
re.search("c", "abcdef") # Match
re.split(pattern, string, mixsplit=0, flag=0)
分离字符串如: >>> re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split('\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
>>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
['0', '3', '9']
re.sub(pattern, repl, string, count=0, flags=0)
re.sub()可以替换匹配到的字符串,其中repl可以为一个函数. >>>re.sub('I', 'You', 'I will be replaced')
'You will be replaced'
- Python3之re模块
- Python3 RE模块
- Python3 re模块
- Python3.2-re模块之常用正则记录
- python3 正则模块 re.compile、re.match、re.search 方法
- Python模块之RE
- python模块之re
- python之re模块
- Python3标准库(二) re模块
- 正则表达式 python3.x (re模块)
- Python3的正则表达式,re模块
- Python3正则表达式(二)re模块
- python学习之re模块
- python模块系列之 - re
- Python基础之re模块
- Python之re模块详解
- python模块系列之-re
- Python学习之re模块
- WindowsForm多线程设置控件通用解决方案
- 探究php底层运行机制
- 关于用户自动登录的问题
- C#随记5
- 2016.11.10 js(复习3完)
- Python3之re模块
- java练习(小数输出)ZZULIOJ-1008: 美元和人民币
- Hadoop架构介绍——MapReduce的体系结构
- 白平衡 matlab 程序 0.01%最小值
- 二、什么是MongoDB
- Android的工程依赖
- AndroidSocket通信实现
- flume实时接收kafka消息并保存至HDFS
- mysql与事务