专业的编程技术博客社区

网站首页 > 博客文章 正文

第45讲 正则表达式(下)(正则表达式^用法)

baijin 2024-09-12 11:01:30 博客文章 6 ℃ 0 评论

小朋友们好,大朋友们好!

我们今天继续学习正则表达式。

如果小朋友暂时用不到这部分内容,可以先跳过,后面用到了再学习。

今天要学习的内容如下:

Python中的正则表达式

匹配字符串

替换字符串

分割字符串


Python中的正则表达式

Python中使用re模块进行正则表达式操作,需要导入re模块。

import re


匹配字符串

匹配字符串也称查找字符串,有三种形式:

match()

search()

findall()


match方法从字符串的开始处进行匹配。

如果在起始位置匹配成功,返回Match对象,否则返回None。

格式如下:

re.match(pattern,string,[flags])

pattern:模式字符串

string:要进行匹配的字符串

flags:可选参数,用于控制匹配模式,常用标志如下:

A(ASCII):对于\w、\W、\b、\B、\d、\D、\s、\S只进行ASCII匹配(适用于Python3.x)

I(IGNORECASE):不区分大小写

M(MULTILINE):将^和$用于包括整体字符串的开始和结尾的每一行(默认情况下,仅使用于整个字符串的开始和结尾处)

S(DOTALL):使用(.)字符匹配所有字符,包括换行符

X(VERBOSE):忽略模式字符串中未转义的空格和注释

返回值Match对象包含了匹配值的位置和匹配数据。起始位置使用方法start,结束位置使用方法end,通过span方法可以返回匹配的元组,string属性可以获取要匹配的字符串

猫妹的测试代码见45.2.1.py

import re

def print_match_info(match):
    if match != None:
        print('匹配值的起始位置是:',match.start())
        print('匹配值的结束位置是:',match.end())
        print('匹配值的元组是:',match.span())
        print('要匹配的字符串:',match.string)
        print('匹配数据:',match.group())


#模式字符串
pattern = r'long\w?'

#要匹配的字符串
string = 'long long ago.'

#匹配字符串,不区分大小写
match = re.match(pattern,string,re.I)

print('first match')
print(match)
print_match_info(match)

#要匹配的字符串
string = 'a long river'

#匹配字符串,不区分大小写
match = re.match(pattern,string,re.I)

print('second match')
print(match)
print_match_info(match)


findall用于在整个字符串中搜索所有符合正则表达式的表达式,以列表形式返回。

如果匹配成功,返回包含匹配结构的列表。否则返回空列表。

格式如下:

re.findall(pattern,string,[flags])

参数同match中的参数含义一致。

猫妹的测试代码见45.2.2.py

import re

def print_match_info(match):
    if match != None:
        print('匹配值的起始位置是:',match.start())
        print('匹配值的结束位置是:',match.end())
        print('匹配值的元组是:',match.span())
        print('要匹配的字符串:',match.string)
        print('匹配数据:',match.group())


#模式字符串
pattern = r'long\w?'

#要匹配的字符串
string = 'long long ago.'

#匹配字符串,不区分大小写
match = re.search(pattern,string,re.I)

print('first match')
print(match)
print_match_info(match)

#要匹配的字符串
string = 'a long river'

#匹配字符串,不区分大小写
match = re.search(pattern,string,re.I)

print('second match')
print(match)
print_match_info(match)


search方法用于在整个字符串中搜索第一个匹配的值。

如果在起始位置匹配成功,返回Match对象,否则返回None。格式如下:

re.search(pattern,string,[flags])

参数同match中的参数含义一致。

猫妹的测试代码45.2.3.py

import re

def print_match_info(match):
    if match != None:
        print('匹配值的起始位置是:',match.start())
        print('匹配值的结束位置是:',match.end())
        print('匹配值的元组是:',match.span())
        print('要匹配的字符串:',match.string)
        print('匹配数据:',match.group())


#模式字符串
pattern = r'long\w?'

#要匹配的字符串
string = 'long long ago.'

#匹配字符串,不区分大小写
match = re.findall(pattern,string,re.I)

print('first match')
print(match)


#要匹配的字符串
string = 'a long river'

#匹配字符串,不区分大小写
match = re.findall(pattern,string,re.I)

print('second match')
print(match)


替换字符串

sub方法可用于字符串替换。

格式如下:

re.sub(pattern,repl,string,count,flags)

pattern:模式字符串

repl:要替换的字符串

string:要被查找替换的原字符串

count:可选参数,匹配后替换的最大次数,默认值是0,表示替换所有

flags:可选参数,控制匹配方式,含义同match参数中的flags

猫妹的测试代码见45.3.py

import re

'''
对手机号中间四位进行隐藏
'''
tel = '13312345678'
result=re.sub(r'(\d{3})(\d{4})(\d{4})','\g<1>****\g<3>',tel)
print(result)


分割字符串

split方法用于实现根据正则表达式分割字符串,以列表形式返回。

同split方法类似,不同的是分割字符由模式字符串指定。

语法如下:

re.split(pattern,string,[maxsplit],[flags])

pattern:模式字符串,要匹配的正则表达式转换而来

string:要匹配的字符串

maxsplit:可选参数,最大拆分次数

flags:可选参数,控制匹配方式,含义同match参数中的flags

猫妹的测试代码见45.4.py

import re

str1='@唐三藏 @孙悟空 @猪八戒 @沙和尚'

#使用空格和@或@来分割字符串
pattern=r'\s*@'

list1=re.split(pattern,str1)
print(list1)


好了,今天的学习就到这里!

我们下次见!

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表