python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python正则表达式

python成长技能之正则表达式示例详解

作者:杰仔正在努力

这篇文章主要介绍了python正则表达式的相关资料,涵盖了正则表达式的基本语法、字符匹配、重复出现数量、字符集、边界匹配、组、贪婪与非贪婪匹配等内容,并通过实际例子展示了如何使用正则表达式进行字符串匹配和处理,需要的朋友可以参考下

一、认识正则表达式

如何在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 的符号来引用 

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

代码解析

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']
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

总结:

七、正则表达式之贪婪与非贪婪

贪婪匹配

默认情况下,大多数量词都是贪婪的,这意味着它们会尽可能多地匹配字符。例如:

假设我们有一个字符串,包含一些 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']

总结 

到此这篇关于python成长技能之正则表达式的文章就介绍到这了,更多相关python正则表达式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文