import re #a='python 1111java678php' # r=re.findall('[a-z]{3,6}',a) # print(r) # ['python', 'java', 'php'] # Process finished with exit code #贪婪与非贪婪 python采用贪婪匹配 匹配更多 #非贪婪 # r=re.findall('[a-z]{3,6}?',a) # print(r) # ['pyt', 'hon', 'jav', 'php'] # Process finished with exit code 0a # a='pytho0python1pythonn2' # # r=re.findall('python{1,2}?',a) # # print(r) #['python', 'python'] #验证qq号位数为4-8位 #qq='100000001' # r=re.findall('^\d{1,9}$',qq) # print(r) #结果 ['100000001'] language='pythonc#javac#phpc#' def convert(value): matched=value.group() return '!!'+matched+'!!' r=re.sub('c#',convert,language) print(r) # python!!c#!!java!!c#!!php!!c#!! #
# Process finished with exit code 0
import re # s='83C72D1D8E67' # r=re.match('\d',s) # print(r) # # r1=re.search('\d',s) # print(r1.group()) # <_sre.SRE_Match object; span=(0, 1), match='8'> # 8 # s='life is short,i use python' # r=re.search('life.*python',s) # print(r.group()) # life is short,i use python # s='life is short,i use python' # r=re.search('life(.*)python',s) # print(r.group(1)) #is short,i use s='life is short,i use python, i love python' r=re.search('life(.*)python(.*)python',s) #print(r.group(0,1,2)) ('life is short,i use python, i love python', ' is short,i use ', ', i love ') print(r.groups()) (' is short,i use ', ', i love ')