Python正则表达

```
# -*- coding:utf-8 -*-
import re

re - Support for regular expressions (RE).
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。
re 模块使 Python 语言拥有全部的正则表达式功能。
compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象,该对象拥有一系列方法用于正则表达式匹配和替换。
re 模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数。
```
```
match 正则表达式模式与字符串的开头匹配.
fullmatch 正则表达式模式与所有字符串匹配
search 在字符串中搜索模式的存在
sub 替换在字符串中找到的模式匹配.
subn 与sub相同,但也返回所做的替换次数
split 按照模式的出现位置分割字符串后返回列表
findall 以列表的形式返回能全部匹配到的子串
finditer 返回一个迭代器,为每个匹配产生一个Match对象
compile 将模式编译为Pattern对象
purge 清除正则表达式缓存
escape 反斜杠字符串中的所有非字母数字
```
```
'1. 匹配任意单个字符, 换行符 除外'
pattern01 = re.compile(pattern=r'.', flags=0)
ma01 = re.match(pattern=pattern01,string='hello china!')

'2. 匹配任意单个字符, 包括换行符 '
pattern02 = re.compile(r'.',flags=re.S)
ma02 = re.match(pattern=pattern02, string=' hello china!', flags=0)

'3. 匹配指定的字符串 '
pattern03 = re.compile(pattern=r'hello')
ma03 = re.match(pattern=pattern03,string='hello world!')

'4. 匹配任意单个小写字母 '
pattern04 = re.compile(r'[a-z]')
ma04 = re.match(pattern=pattern04,string='hello world!')

'5. 匹配任意单个字母 '
pattern05 = re.compile(r'[a-zA-Z]')
ma05 = re.match(pattern=pattern05,string='Hello World!')

'6. 匹配任意单个数字'
pattern06 = re.compile(r'[0-9]') or re.compile(r'd')
ma06 = re.match(pattern06,'2018-10-10')

'7. 匹配任意单个字母数字 '
pattern07 = re.compile(r'w' or r'[a-zA-Z0-9]')
ma07 = re.match(pattern07,'hello world')

'8. 匹配任意单个非字母数字'
pattern08 = re.compile(r'W') or re.compile(r'[^w]')
ma08 = re.match(pattern08,'<hello world>')

'9. 匹配任意单个非数字'
pattern09 = re.compile(r'D' or r'[^d]')
ma09 = re.match(pattern09, 'hello world!')

'10. 匹配任意单个空白字符 f v 空格'
pattern10 =re.compile(r's')
ma10=re.match(pattern10, ' hello world!')

'11. 匹配任意单个非空白字符'
pattern11 = re.compile(r'S')
ma11 = re.match(pattern11, 'hello china!')

'12. 匹配特殊字符 . * + ? 等,使用转义符号/ '
pattern12 =re.compile(r'+')
ma12 = re.match(pattern12, '+*-?hello china!')

'13. 匹配任意字符0次或无限次'
pattern13 = re.compile(r'.*',flags=re.S)
ma13 = re.match(pattern13, 'hello china!')

'14. 匹配任意字母1次或无限次'
pattern14 = re.compile(r'[a-zA-Z]+')
ma14 = re.match(pattern14,'hello world!')

'15. 匹配任意数字0次或1次'
pattern15 = re.compile(r'd?')
ma15 = re.match(pattern15,'hello china')


'16. 匹配前一个字符m次'
pattern16 = re.compile(r'[a-z]{2}')
ma16 = re.match(pattern16,'hello china')

'17. 匹配前一个字符m-n次'
pattern17 =re.compile(r'[a-z]{2,4}')
ma17 =re.match(pattern17,'hello china')

'18. 非贪婪模式匹配, 尽可能的少匹配或不匹配 *? +? ?? {m,n}?'
pattern18 = re.compile(r'w+?')
ma18 =re.match(pattern18,'hello wolrd')

'19. 匹配模式分组, 使用编号引用'
pattern19 =re.compile(r'<(w+>)w+</1')
ma19 = re.match(pattern19,'<book>python3</book>')

'20.匹配模式分组,并其别名, 用别名引用'
pattern20 = re.compile(r'<(?P<name>w+>)w+</(?P=name)')
ma20 = re.match(pattern20,'<book>python3</book>')

'21. 匹配字符串的开头'
pattern21 = re.compile(r'^[a-zA-z_].*')
ma21 = re.match(pattern21,'sutdent1 = 12')

'22. 匹配字符串的结尾'
pattern22 = re.compile(r'.com$')
ma22 = re.search(pattern22,'www.test2018.com')

'23. 仅仅匹配字符串开头 A'
'24. 仅仅匹配字符串结尾 '
```
```
're.I 使匹配对大小写不敏感'
're.L 做本地化识别(locale-aware)匹配'
're.M 多行匹配,影响 ^ 和 $'
're.S 使 . 匹配包括换行在内的所有字符'
're.U 根据Unicode字符集解析字符。这个标志影响 w, W, , B.'
're.X 该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解'
```

原文地址:https://www.cnblogs.com/xiaofeifei-wang/p/10492885.html