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