-
Re compile(pattern,flags=0)
不是必须的
-
MatchObj/None Re.match(string)
如果匹配失败返回None,不能用flags,从开头开始找,没找到就不找了
-
MatchObj/None match(pattern,string,flags=0)
如果匹配失败返回None,从开头开始找,没找到就不找了
import re
re=re.compile(r'hello')
print(re) --> re.compile('hello')
print(type(re)) --> <class 're.Pattern'>
result=re.match("hello world")
print(result) --> <re.Match object; span=(0, 5), match='hello'>
print(type(result)) --> <class 're.Match'>
print(re.match("Hello world")) --> None
-
List Re.findall(string)
不能使用flags
-
List findall(pattern,string[,flags])
import re
list=re.findall(r'[a-zA-Z]+',"one1 two2 three3 four4")
print(list) --> ['one', 'two', 'three', 'four']
print(type(list)) --> <class 'list'>
-
MatchObj/None Re.search(string)
如果没找到返回None,不能用flags,从开头开始找,没找到就向后面找
-
MatchObj/None search(pattern,string,flags=0)
如果没找到返回None,从开头开始找,没找到就不找了
import re
result=re.search(r'world',"Hello world")
print(result) --> <re.Match object; span=(6, 11), match='world'>
print(type(result)) --> <class 're.Match'>
-
string MactchObj.group([num])
-
Tuple MatchObj.grounps()
-
Dict MatchObj.groupdict()
import re
matchObj=re.search(r'(\d{6})(\d{4})((\d{2})(\d{2}))\d{2}\d{1}([0-9]|X)',"425643200001013458")
matchObj.group() --> '425643200001013458'
matchObj.group(2) --> '2000'
matchObj.groups() --> ('425643', '2000', '0101', '01', '01', '8')
matchObj=re.search(r'(\d{6})(?P<year>\d{4})(?P<month>\d{2})(?P<day>\d{2})(\d{2})(\d{1})([0-9]|X)',"425643200001013458")
matchObj.groups() --> ('425643', '2000', '01', '01', '34', '5', '8')
matchObj.groupdict() --> {'year': '2000', 'month': '01', 'day': '01'}
-
List split(pattern,string,max=0)
-
List Re.split(string,max=0)
-
String Re.sub(replace,string,max=0)
-
String sub(pattern,repalce/function/pattern,string,max=0)
import re
list=re.split(r'[a-zA-Z]',"one1two2three3")
type(list) --> <class 'list'>
list --> ['', '', '', '1', '', '', '2', '', '', '', '', '3']
list=re.split(r'\d+',"one1two2three3")
list --> ['one', 'two', 'three', '']
list=re.split(r'\d+',"one1two2three3",2)
list --> ['one', 'two', 'three3']
string=re.sub(r'\d',"#","one1two2three3")
string --> 'one#two#three#'
string=re.sub(r'(\w+) (\w+)',r'\2 \1',"hello python")
string --> 'python hello'
string=re.sub(r'(\w+) (\w+)',lambda m: m.group(2).upper()+' '+m.group(1),"hello python")
string --> 'PYTHON hello'