python成长技能之正则表达式
文章目录
一、认识正则表达式
正则表达式(英语:Regular Expression,常简写为regex、regexp或RE),又称正则表示式、正则表
示法、规则表达式、常规表示法,是计算机科学的一个概念
正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正
则表达式通常被用来检索、替换那些符合某个模式的文本
灵活性、逻辑性和功能性非常强;
可以迅速地用极简单的方式达到字符串的复杂控制
如何在python中使用正则表达式—-findall方法
python中,要使用正则表达式,需要导入re模块,基本格式如下:
re.findall(pattern, string, flags=0)
函数参数说明
flags可选值如下
举例,使用findall()方法
import re
str = "hello,my name is jie"
result = re.findall("jie",str)
print(result)
打印结果
['jie']
在python中使用正则表达式—-match方法
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none
import re
str = "hello,my name is jie"
# result = re.findall("jie",str)
# print(result)
match = re.match("hello",str)
print(match.group(0))
hello
要获取匹配的结果,可以使用group(n),匹配结果又多个的时候,n从0开始递增
当匹配结果有多个的时候,也可以使用groups()一次性获取所有匹配的结果
re.search方法
re.search 扫描整个字符串并返回第一个成功的匹配
import re
s = 'hello world hello'
result = re.search('hello', s)
print(result.group(0))
二、使用正则表达式匹配单一字符
import re
str = "12hellowordhello12"
result = re.findall("\d",str)
print(result)
打印结果
['1', '2', '1', '2']
import re
str = "12hellowordhello12"
result = re.findall("\D",str)
print(result)
打印结果
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'd', 'h', 'e', 'l', 'l', 'o']
import re
str = "12hellowordhello12" + chr(12)
result = re.findall("\f",str)
print(result)
打印结果
['\x0c']
import re
str = "hello word my name is jie"
result = re.findall("/n",str)
print(result)
打印结果
[]
三、正则表达式之重复出现数量匹配
import re
s = "hello world helloo hell"
print(re.findall('hello*', s))
['hello', 'helloo', 'hell']
import re
s = "hello world helloo hell"
print(re.findall('hello+', s))
['hello', 'helloo']
import re
s = "hello world helloo hell"
print(re.findall('hello?', s))
['hello', 'hello', 'hell']
import re
s = "hello world helloo hell helloo hellooo helloo helloo"
print(re.findall('hello{2}', s))
['helloo', 'helloo', 'helloo', 'helloo', 'helloo']
import re
s = "hello world helloo hell helloo hellooo helloo helloo"
print(re.findall('hello{2,}', s))
['helloo', 'helloo', 'hellooo', 'helloo', 'helloo']
import re
s = "hello world helloo hell helloo hellooo helloo helloo"
print(re.findall('hello{2,3}', s))
['helloo', 'helloo', 'hellooo', 'helloo', 'helloo']
四、使用正则表达式匹配字符集
import re
str = "110,120,130,230,250,160"
result = re.findall("1[1-9]0",str)
print(result)
['110', '120', '130', '160']
import re
str = "110,120,130,230,250,160"
result = re.findall("1[^1-9]0",str)
print(result)
[]
五、正则表达式之边界匹配
import re
str = "hello jiejie"
result = re.findall("^he",str)
print(result)
['he']
import re
str = "hello jiejie e e e"
result = re.findall("e$",str)
print(result)
['e']
import re
str = "hello jiejie hel"
result = re.findall(r'\bhe',str)
print(result)
['he', 'he']
六、正则表达式之组
将括号:() 之间的表达式定义为“组”(group),并且将匹配这个表达式的字符保存到一个临时区域(一个正则表达式中最多可以保存9个),它们可以用 \1 到\9 的符号来引用
- 假设我们有一个字符串,包含一些日期格式,如 “2023-10-01”,我们想分别捕获年、月和日
import re
# 捕获组示例
text1 = "Today's date is 2023-10-01."
pattern1 = r'(\d{4})-(\d{2})-(\d{2})'
match1 = re.search(pattern1, text1)
if match1:
year = match1.group(1)
month = match1.group(2)
day = match1.group(3)
print(f'Year: {year}, Month: {month}, Day: {day}')
# 输出结果
Year: 2023, Month: 10, Day: 01
代码解析
- 假设我们有一个字符串,包含一些电话号码,格式为 “123-456-7890”,我们想匹配这种格式,但不需要捕获每个部分
import re
text = "Phone number: 123-456-7890."
pattern = r'(?:\d{3}-){2}\d{4}'
match = re.search(pattern, text)
if match:
print(f'Matched phone number: {match.group(0)}')
# 输出结果
Matched phone number: 123-456-7890
- 假设我们有一个字符串,包含一些重复的单词,我们想找到这些重复的单词
import re
text = "This is a test test of repeated repeated words words."
pattern = r'\b(\w+)\b\s+\1\b'
matches = re.findall(pattern, text, re.IGNORECASE)
if matches:
print(f'Repeated words: {matches}')
# 输出结果
Repeated words: ['test', 'repeated', 'words']
- 假设我们有一个字符串,包含一些日期格式,如 “2023-10-01”,我们想分别捕获年、月和日,并使用命名组
import re
text = "Today's date is 2023-10-01."
pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
match = re.search(pattern, text)
if match:
year = match.group('year')
month = match.group('month')
day = match.group('day')
print(f'Year: {year}, Month: {month}, Day: {day}')
# 输出结果
Year: 2023, Month: 10, Day: 01
总结:
- 捕获组:使用 () 定义,可以捕获匹配的部分
- 非捕获组:使用 (?:…) 定义,仅用于分组和逻辑处理
- 反向引用:使用 \n 引用第 n 个捕获组
- 命名组:使用 (?P…) 定义,可以按名称引用捕获组
七、正则表达式之贪婪与非贪婪
贪婪匹配
默认情况下,大多数量词都是贪婪的,这意味着它们会尽可能多地匹配字符。例如:
假设我们有一个字符串,包含一些 HTML 标签,我们想提取标签内的内容
import re
text = '<div>Hello</div><div>World</div>'
pattern = r'<div>(.*)</div>'
matches = re.findall(pattern, text)
print(matches)
# 输出结果
['Hello</div><div>World']
在这个例子中,.* 是贪婪的,它会尽可能多地匹配字符,因此匹配结果是从第一个 < div>到最后一个< /div>之间的所有内容
非贪婪匹配
非贪婪匹配(也称为懒惰匹配)是指量词会尽可能少地匹配字符。非贪婪匹配可以通过在量词后面加上 ? 来实现。例如:
import re
text = '<div>Hello</div><div>World</div>'
pattern = r'<div>(.*?)</div>'
matches = re.findall(pattern, text)
print(matches)
# 输出结果:
['Hello', 'World']
作者:杰仔正在努力