Python 正则表达式

def match(pattern, string, flags=0):
"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).match(string)

import re
print (re.match('www','www.baidu.com'))  
结果:<_sre.SRE_Match object; span=(0, 3), match='www'>
print (re.match('www','www.baidu.com').group())
结果:www
print (re.match('w.b','www.baidu.com'))
结果:None

结论:匹配开始的字符串,返回一个object的对象,要想查看匹配的结果,需要用到group()方法

def fullmatch(pattern, string, flags=0):
"""Try to apply the pattern to all of the string, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).fullmatch(string)
print (re.fullmatch('www.baidu.com','www.baidu.com').group())
结果:www.baidu.com
print (re.fullmatch('www','www.baidu.com'))
结果:None

def search(pattern, string, flags=0):
"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).search(string)
print (re.search('w.b','www.baidu.com'))
结果:<_sre.SRE_Match object; span=(2, 5), match='w.b'>
print (re.search('w.b','www.baidu.com').group())
结果:w.b

print (re.search('wwa','www.baidu.com'))
结果:None

def sub(pattern, repl, string, count=0, flags=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the match object and must return
a replacement string to be used."""
return _compile(pattern, flags).sub(repl, string, count)
import re
inputStr = "hello 111 world 222"
outputStr = re.sub("d+","333",inputStr)
print(outputStr)
结果:hello 333 world 333
inputStr = "hello 111 world 222 hello hello hello hello"
outputStr = re.sub("(llo)","333",inputStr,2)
print(outputStr)
结果:he333 111 world 222 he333 hello hello hello

import re;
inputStr = "hello-111 world 222"
outputStr = re.sub("w+-d+","333",inputStr)
print(outputStr)
结果:333 world 222
结论:w+表示一个字母或数字组成的字符串,d+表示至少一个数字,-表示中间的连接符-

inputStr = "hello-111 world 222"
outputStr = re.sub("[A-Za-z]w*","333",inputStr)
print(outputStr)
结果:333-111 333 222
结论:前面是字母,后面是字母或者数字

inputStr = "hello-111-222"
outputStr = re.sub("d{3}-d{3}","333",inputStr)
print(outputStr)
结果:hello-333
结论:三个数字,三个数字,中间用-连接起来

inputStr = "lij1@hkbea.com"
outputStr = re.sub("w+@w+.com","333",inputStr)
print(outputStr)
结果:333









原文地址:https://www.cnblogs.com/python-study/p/5476070.html