python之正则表达式

hpl201314 2020-11-12 17:02:14
Python 正则表达式 正则 表达式 表达


1,什么是正则表达式?

正则表达式(regular expression)是用来简洁表达一组字符串的表达式。

2,作用是什么?

①表达文本类型的特征。 ②同时查找或替换一组字符串。 ③匹配字符串的全部或部分。

3,常用的操作符:

 

操作符 说明 例子
. 表示任何单个字符  
[] 字符集,对单个字符给出取值范围  [abc]表示a,b,c,[a-z]表示a-z单个字符
[^] 非字符集,对单个字符给出排除范围  [^abc]表示除a,b,c之外的单个字符
* 前一个字符0次或无限次扩展 abc*表示ab,abc,abcc,abccc等等
+ 前一个字符1次或无限次扩展 abc+表示abc,abcc,abccc等等
前一个字符出现或者不出现 abc表示ab,abc
| 左右表达式任意一个 abc|def表示abc,def
{m} 扩展前一个字符m次 ab{4}c表示abbbbc
{m,n} 扩展前一个字符m到n次,含m,n ab{1,2}c表示abc,abbc
^ 匹配字符串开头 ^abc表示abc且在字符串的开头
$ 匹配字符串结尾 abc$表示abc且在字符串的结尾
() 分组标记,内部只能使用 | 操作符 (abc)表示abc,(abc | def)表示abe、def
\d 数字,等价于[0,9]  
\w 单词字符,等价于[A-Za-z0-9_]  

4,正则表达式的一些语法实例

正则表达式 对应的字符串
P(Y|YT|YTH|YTHO)?N "PN","PYN","PYTN","PYTHN","PYTHON"
PYTHON+ "PYTHON","PYTHONN","PYTHONNN".......
PY[TH]ON "PYTON","PYHON"
PY[^TH]?ON "PYON","PYAON","PYBON","PYCON"......
PY{:3}N "PN","PYN","PYYN","PYYYN"

5,经典的正则表达式实例

^[A-Za-z]+$ 由26个字母组成的字符串
^[A-Za-z0-9]+$ 由26个字母和数字组成的字符串
^-?\d+$ 整数形式的字符串
^[0-9]*[1-9][0-9]*$ 正整数形式的字符串
[1-9]\d{5} 中国境内的邮政编码
[\u4e00-\u9fa5] 匹配中文字符
\d{3}-\d{8}|\d{4}-\d{7} 国内的电话号码,010-12345678
[1-9]?\d 0-99
1\d{2} 100-199
2[0-4]\d 200-249
25[0-5] 250-255

(([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5]).){3}([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])

匹配ip地址

6,re库的基本使用

 

re库的主要功能函数
re.search() 在一个字符串中搜索匹配正则表达式的第一个位置,返回match对象
re.match() 从一个字符串的开始位置起匹配正则表达式,返回match对象
re.findall() 搜索字符串,以列表类型返回全部能匹配的子串
re.split() 将一个字符串按照正则表达式匹配结果进行分割,返回列表类型
re.finditer() 搜索字符串,返回一个匹配结果的迭代类型,每个迭代元素是match对象
re.sub() 在一个字符串中替换所有匹配正则表达式的子串,返回替换后的字符串

 

①search(pattern, string, flags=0)

pattern:正则表达式的字符串或原生字符串表示
string:待匹配字符串
flags:正则表达式使用时的控制标记

1 import re
2 match = re.search(r"[1-9]\d{5}", "haha 723300")
3 if match:
4 print(match.group())
5
6 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
7 723300
8
9 Process finished with exit code 0
search

②match(pattern,string,flags=0)

需要注意的是 match 函数是从字符串开始处开始查找,如果开始处不匹配,则不再继续寻找,若找到返回值为 一个 match 对象,找不到时返回 None

 1 import re
 2 match = re.match(r"[1-9]\d{5}", "haha 723300")
 3 print(type(match))
 4 match = re.match(r"[1-9]\d{5}", "723300 haha")
 5 if match:
 6 print(match.group())
 7
 8 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
 9 <class 'NoneType'>
10 723300
11
12 Process finished with exit code 0
match

可见search与match的区别在于:
match要求待匹配的子串必须在字符串的起始位置,否则查找不到,而search则无此要求

③findall(pattern,string,flags=0)

 1 import re
 2 c = re.findall(r"[1-9]\d{5}", "haha723300 xixi612203")
 3 print(type(c))
 4 print(c)
 5
 6 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
 7 <class 'list'>
 8 ['723300', '612203']
 9
10 Process finished with exit code 0
findall

④split(pattern,string,maxsplit=0,flags=0)

maxsplit:最大分割数,剩余部分作为最后一个元素输出

 1 import re
 2 a = re.split(r"[1-9]\d{5}", "haha723300 xixi612203")
 3 print(type(a))
 4 print(a)
 5
 6 a = re.split(r"[1-9]\d{5}", "haha723300 xixi612203", maxsplit=1)
 7 print(a)
 8
 9 str1 = "name: hpl, age: 18"
10 b = re.split(r'\:|\,', str1)
11 print(b)
12
13
14 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
15 <class 'list'>
16 ['haha', ' xixi', '']
17 ['haha', ' xixi612203']
18 ['name', ' hpl', ' age', ' 18']
19
20 Process finished with exit code 0
split

⑤finditer(pattern,string,flags=0)

 1 import re
 2 for m in re.finditer(r"[1-9]\d{5}", "haha723300 xixi612203"):
 3 if m:
 4 print(m.group())
 5
 6 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
 7 723300
 8 612203
 9
10 Process finished with exit code 0
finditer

⑥sub(pattern,repl,string,count=0,flags=0)

repl:替换匹配字符串的字符串
count:匹配的最大替换次数

1 import re
2 m = re.sub(r"[1-9]\d{5}", "love", "haha723300 xixi612203")
3 if m:
4 print(m)
5
6 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
7 hahalove xixilove
8
9 Process finished with exit code 0
sub

7,re库的match对象

属性:
string 待匹配的文本
re 匹配时使用的pattern对象(正则表达式)
pos 正则表达式搜索文本的开始位置
endpos 正则表达式搜索文本的结束位置

方法:
group() 获得匹配后的字符串
start() 匹配字符串在原始字符串的开始位置
end() 匹配字符串在原始字符串的结束位置
span() 返回(start)(end)

 1 import re
 2 match = re.search(r"[1-9]\d{5}", "haha723300 xixi612203")
 3 print(match.string)
 4 print(match.re)
 5 print(match.pos)
 6 print(match.endpos)
 7 print(match.group())
 8 print(match.start())
 9 print(match.end())
10 print(match.span())
11
12 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
13 haha723300 xixi612203
14 re.compile('[1-9]\\d{5}')
15 0
16 21
17 723300
18 4
19 10
20 (4, 10)
21
22 Process finished with exit code 0
re库的match对象

8,re库的贪婪匹配和最小匹配

①re库默认采用贪婪匹配,即输出匹配最长的子串

1 import re
2 match = re.search(r'PY.*N','PYANBNCNDN')
3 print(match.group())
4
5 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
6 PYANBNCNDN
7
8 Process finished with exit code 0
贪婪匹配

②最小匹配的方法:在扩展操作符后加?

最小匹配操作符
操作符 说明
*? 前一个字符0次或无限次扩展,最小匹配
+? 前一个字符1次或无限次扩展,最小匹配
?? 前一个字符0次或1次扩展,最小匹配
[m,n]? 扩展前一个字符m至n次(含n),最小匹配
1 import re
2 match = re.search(r'PY.*?N','PYANBNCNDN')
3 print(match.group())
4
5 G:\Project1\venv\Scripts\python.exe G:/Project1/practice/lianxi2.py
6 PYAN
7
8 Process finished with exit code 0
最小匹配

 

 


 

 

 

 

 

 

 

版权声明
本文为[hpl201314]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/hpl201314/p/13907159.html

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database