Python:正则表达式举例说明

2021-10-19  本文已影响0人  闲酿星河
re.findall
import re
# 字符集
a = 'abc, acc, adc, aec, afc'
print(re.findall('ac', a))                >>>['ac']
print(re.findall('a[a-z]c', a))           >>>['abc', 'acc', 'adc', 'aec', 'afc']
print(re.findall('a[^e,f]c', a))          >>>['abc', 'acc', 'adc']
概括字符集
b = 'python&__123C++ \n'
print(re.findall('\s', b))          >>>[' ', '\n']
数量词
c = 'python123java123php2332'
print(re.findall('[a-z]{3,6}', c))      >>>['python', 'java', 'php']
d = 'pytho&pythonn&python ++'
print(re.findall('python*', d))    >>>['pytho', 'pythonn', 'python']
print(re.findall('python+', d))    >>>['pythonn', 'python']
print(re.findall('python?', d))    >>>['pytho', 'python', 'python']
边界值 ^ $
e = '10000001'
print(re.findall('^000', e))
print(re.findall('^100', e))
# 首尾加上 ^ $ 表示匹配整个字符串 为 4~9个数字
print(re.findall('^\d{4,9}$', e))    
print(re.findall('0$', e))
f = 'pythonpythonPythonPythonpppython'
print(re.findall('(python){2}', f))   # ()表示且 匹配时需括号内内容都出现
print(re.findall('[python]{3}', f))   # []表示或 匹配时需要出现3个[]内的字符即可匹配
匹配模式参数
g = 'Python\njava'
print(re.findall('python', g, re.I))  # 无视大小写
print(re.findall('python.{2}', g, re.I|re.S))  # 无视大小写,  . 匹配全部参数
re.sub
import re


def cover(value):
    mas = value.group()
    if int(mas)>=6:
        return '9'
    else:
        return '0'


a = 'python|java|python'
b = 'A23B8c09x9'
r1 = re.sub('python', 'java', a, 1)  # 1:仅替换匹配到的第一个;0:替换所有匹配到的
r2 = re.sub('\d', cover, b,)         # 将字符串内的数字按规则替换 函数返回的值为替换的字符。
r3 = a.replace('python', 'java')
import re
time = '2019/02/01  this is a time'
t1 = re.sub(r'(\d{4})/(\d{2})/(\d{2})', r'\2/\3/\1', time)
print(t1)   >>> 02/01/2019  this is a time

t2 = re.sub(r'(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})', r'\g<month>/\g<day>/\g<year>', time)
print(t2)   >>> 02/01/2019  this is a time
re.match 和 re.search
import re

a = 'A01BBw3'

# match 从第一个字符开始匹配
r1 = re.match('\d', a)  # 第一个字符不是数字,返回的none

# search 匹配到第一个能匹配到的字符后就停止
r2 = re.search('\d', a)
print(r1)
print(r2)
group()
b = 'life is short,i use python,i love python'

b1 = re.search('life(.*)python(.*)python', b)
print(b1.group(0))
print(b1.group(1))
print(b1.group(2))

贪婪与非贪婪
 python默认以贪婪方式匹配表达式   ?前面表示一个范围时表示非贪婪
c = 'python123java123php2332C'
# 默认贪婪
print(re.findall('[a-z]{3,6}', c))     >>>['python', 'java', 'php']
# 非贪婪
print(re.findall('[a-z]{3,6}?', c))    >>>['pyt', 'hon', 'jav', 'php']
上一篇下一篇

猜你喜欢

热点阅读