正则表达式(1)
import re text = "Hi,I am Shirley Hilton.I am his wife." m = re.findall(r"hi",text) m = re.findall(r"hi",text) if m: print m else: print 'not match'
正则表达式(2)
import re text = "Hi,I am Shirley Hilton.I am his wife." m = re.findall(r"hi",text) m = re.findall(r"hi",text) m = re.findall("[Hh]i",text) m = re.findall("hi",text) m = re.findall("hi",text) m = re.findall(r"[Hh]i",text) if m: print m else: print 'not match'
print "hi" print r"hi" print "\bhi"
import re text = "Hi,I am Shirley Hilton.I am his wife." m = re.findall("i.",text) m = re.findall(".",text) m = re.findall("I.*e",text) #贪婪匹配 #m = re.findall("I.*?e",text) #懒惰匹配 if m: print m else: print 'not match'
正则表达式(3)
最后留一道习题:
从下面一段文本中,匹配出所有s开头,e结尾的单词。
site sea sue sweet see case sse ssee loses
import re text = "site sea sue sweet see case sse ssee loses" m = re.findall(r"sS*?e",text) #不包括空白字符 m = re.findall("sS.*?e",text) m = re.findall(r"s.*?e",text) m = re.findall("s.*?e",text) if m: print m else: print 'not match'
正则表达式(4)