Python学习笔记
来源:互联网 发布:网络的逻辑结构 编辑:程序博客网 时间:2024/06/05 16:01
模块
- python 处理正则表达式的模块是 re
import re
匹配
match(pattern, string, flags=0)
用来判断pattern
是否匹配string
,如果匹配,返回的是一个 match
对象,如果不匹配,返回 None
。在 pattern
中可以通过 \num
或者 (?P=group)
引用分组。 Match
对象主要有三个方法:
注:所谓子组,是指正则表达式中用小括号括起来的分组,子组的顺序按照左小括号的顺序来决定。
例子:
#coding=utf-8import rePATTERN = r'(?P<prefix>\d{3})-?(?P<number>\d{8})'PHONE_NUMBER = '18612345678'm = re.match(PATTERN, PHONE_NUMBER)if m is not None: print(m.group()) # 18612345678 print(m.group(0)) # 18612345678 print(m.group(1)) # 186 print(m.group(2)) # 12345678 print(m.groups()) # ('186', '12345678') print(m.groupdict()) # {'prefix': '186', 'number': '12345678'}else: print("Does not match")
搜索
search(pattern, string, flags=0)
用来在 string
中搜索第一次 pattern
匹配的地方。如果找到,返回 match
对象,否则返回 None
:
例子:
#coding=utf-8import rePATTERN = r'12345'TEST_STR = '1112345'm = re.search(PATTERN, TEST_STR)if m is not None: print(m.group()) # 12345else: print("Does not match")
查找
findall(pattern, string[,flags])
查找 string
中出现的所有 pattern
,返回匹配列表。 finditer(pattern, string[, flags])
与 findall
类似,不同的是返回迭代器。
例子:
#coding=utf-8import rePATTERN = r'\d{3}'TEST_STR = '123456789'print(re.findall(PATTERN, TEST_STR)[0]) # ['123', '456', '789']print([m.group() for m in re.finditer(PATTERN, TEST_STR)]) # ['123', '456', '789']
替换
sub(pattern, repl, string, count=0)
将 string
中的 pattern
替换为 repl
,count
表示替换的次数。在 repl
中可以通过 \num
或者 \g<groupname>
来引用 pattern
中的分组。
例子:
#coding=utf-8import rePATTERN = r'(\w)\1' # 或者 r'(?P<coupled>\w)(?P=coupled)'REPL = r'\1\1\1' # 或者 r'\g<coupled>\g<coupled>\g<coupled>'TEST_STR = 'A good cook could cook as much cookies as a good cook who could cook cookies.'print(re.sub(PATTERN, REPL, TEST_STR))# A goood coook could coook as much coookies as a goood coook who could coook coookies.
分割
split(pattern, string, max=0)
将 string
按照 pattern
分割,最多 max
次。
例子:
#coding=utf-8import rePATTERN = r'\s'TEST_STR = '123 456\t\t789\n'print(re.split(PATTERN, TEST_STR))# ['123', '', '', '', '456', '', '789', '']# remove blank using filter()print([item for item in filter(lambda x: x!='', re.split(PATTERN, TEST_STR))])# ['123', '456', '789']
标志
re.I
, re.IGNORECASE
忽略大小写 re.L
, re.LOCALE
\w, \W, \b, \B, \s, \S 本地化 re.M
, re.MULTILINE
^$匹配每一行,而不是整个字符串 re.S
, re.DOTALL
.也匹配\n re.X
, re.VERBOSE
忽略空格和空格后面的#号,多用在注释阅读全文
1 0
- 【python】:python学习笔记
- Python学习笔记-Python起步
- Python学习笔记-Python基础
- Python学习笔记-Python对象
- Python基本概念--Python学习笔记
- Python学习笔记:Python函数
- python学习笔记-python安装
- Python学习笔记--Python基础
- 【Python】python基础学习笔记
- python学习笔记3
- Python学习笔记
- Boost.Python学习笔记
- python学习笔记1
- python学习笔记2
- python学习笔记(2)
- Python学习笔记 Module
- Python学习笔记.
- python学习笔记(1)
- Python 编程核心知识体系
- 操作Word
- 渺小
- 如何优化网站加载速度
- Feasibility of Learning & Training versus Test(林轩田-机器学习)
- Python学习笔记
- IDEA Properties中文unicode转码问题
- 解决lxml安装时“lxml-4.1.0-cp27-cp27m-win32.whl is not a support wheel on this platform”报错
- ZOJ 3987 Numbers (贪心)
- CSS 解決版面撑开、英文断字、避头尾、段落左右边界不齐等方法
- 2017.11.1~11.2模拟题
- SpringCloud教程 | 第五篇: 路由网关(zuul)
- 请求转发和重定向的区别
- Guitar Pro 的双十一特惠活动,正在如火如荼进行中...