菜比之路

走菜比的路,让大牛说去吧
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

【python】正则表达式-re模块

Posted on 2020-02-13 09:48  毕加索的ma  阅读(196)  评论(0编辑  收藏  举报

re.compile(pattern):编译正则表达式,返回编译后的模正则表达式对象,该对象同样拥有match、search等方法。

import re
pattern = re.compile('python') # compile将字符串当做正则表达式来编译
result = pattern.search('hello python!')
print(result)
#>>><re.Match object; span=(6, 12), match='python'>
print(result.group())
#>>>python

re.match(pattern, string):匹配字符串的开头,成功则返回匹配对象,否则返回None

import  re
result = re.match('a', 'abc') # match是从字符串的开头开始匹配
print(result)
#>>><re.Match object; span=(0, 1), match='a'>
print(result.group()) # 并不直接返回匹配成功的字符串,需要使用group()方法
#>>>a
result = re.match('a', 'dabc')#因为开始不是a,所以没匹配成功
print(result)
#>>>None

result = re.match('abc$', 'abc') # $是指被匹配字符的最后一个字母,如果是re.match('ab$', 'abc'),是不能匹配成功的
print(result)
#>>><re.Match object; span=(0, 3), match='abc'>
print(result.group()) # 并不直接返回匹配成功的字符串,需要使用group()方法
#>>>abc

 re.search(pattern, string):从字符串开头开始查找匹配,直到匹配成功,则不再往后继续查找匹配,成功返回匹配对象,否则返回None。

import  re
result = re.search('python', 'abcpythondef') # 在字符串的全文中搜索匹配一次,如果用match会报错
print(result)
#>>><re.Match object; span=(3, 9), match='python'>
print(result.group())
#>>>python
result = re.search('aal?', 'aaexaaa') # ?匹配0次或者1次
print(result)
#>>><re.Match object; span=(3, 9), match='python'>
print(result.group())
#>>>aa

 re.findall(pattern, string):查找匹配字符串中所有内容,返回查找成功的字符串的列表,如果字符串中没有匹配成功的内容,则返回空列表

import  re
result = re.findall('python', 'abc python def python ghi')  ##findall 不需要group来匹配
print(result)
#>>>['python', 'python']

re.split(pattern, string, maxsplit=0):使用匹配成功后的字符串作为“分割符”,返回分割后的字符串列表,maxsplit为分割的次数,默认0不是分割0次,而是分割所有。

import  re
result = re.split('a', '1a2a3a4guyuyun') # 将匹配成功的字符串用作字符串分隔符,返回分隔后的字符串列表
print(result)##split也不需要group匹配
#>>>['1', '2', '3', '4guyuyun']
result = re.split('c', '1a2a3a4guyuyun') # 将匹配成功的字符串用作字符串分隔符,返回分隔后的字符串列表
print(result)##split也不需要group匹配
#>>>['1a2a3a4guyuyun']

re.sub(pattern, repl, string, count=0):使用正则表达式pattern在字符串string中匹配查找,匹配查找成功后使用新字符串repl替换掉匹配成功的字符串,并返回,count为替换次数,默认0不是替换0次,而是替换所有。

import  re
result = re.sub('c', 'z', 'click', 2) # 使用匹配成功的字符串替换成指定的字符串,参数依次为正则表达式,匹配成功后要去替换的字符串,原字符串,2为替换次数
print(result) # 返回替换后的字符串
#>>>zlizk

def sub_no_use_match(match_obj): # 用不到模式对象match_obj,但是该函数必须有这个参数
    print(match_obj.group())#>>>27
    return '36'
result=re.sub(r'\d+', sub_no_use_match, 'Python27') # 以函数返回的字符串替换匹配成功的字符串
print(result)
#>>>Python36

def sub_use_match(match_obj): # 使用模式对象match_obj来返回最终的字符串
    print(match_obj.group())#>>>27
    return match_obj.group() + 'hahahaha'

result=re.sub(r'\d+', sub_use_match, 'Python27')
print(result)
#>>>Python27hahahaha