python练习正则表达式

 
代码:
import re
#print(dir(re))
s="今天天气真好"
print("今天天气真好")
print(re.match("今天",s))
print(re.match("今天",s).group())
print(re.match("天气",s))
print(".")
print(re.match(".","1"))
print(re.match(".","a"))
print(re.match(".","A"))
print(re.match(".","?"))
print("\d")
print(re.match("\d","1"))
print(re.match("\d","a"))
print(re.match("\d","A"))
print(re.match("\d","!"))
print("\w")
print(re.match("\w","123"))
print(re.match("\w","A"))
print(re.match("\w","a"))
print(re.match("\w","@"))
print("^")
print(re.match("^1","123"))
print(re.match("^1","213"))
print(re.match("^1.","12345"))
print(re.match("^1.*","12345"))
print(re.match("^1","23h"))
print(re.match("^1.","23h"))
print(re.match("^1.*","23h"))
print(".")
print(re.match(".","/n"))
print(re.match(".","\n"))
print(re.match("...","abcdefg"))
print("\s")
print(re.match("\s","abc"))
print(re.match("\s","ABC"))
print(re.match("\s","123"))
print(re.match("\s","  123"))
print(re.match("\s","\t  123abc"))
print("$")
print(re.match("$h","123h"))
print(re.match("$1","123h1"))
print(re.match("^1\d$1","121"))
print(re.match("^1\d1$","121"))
print(re.match("1$","121"))
print(re.match("h$","123h"))
print(re.match("^1h$","123h"))
print(re.match("^1\d*h$","123h"))
print("findall")
print(re.findall("h$","123h"))
print("[]")
print(re.match("[0123456789]",""))
print(re.match("[0123456789]","123"))
print(re.match("[0-9]","123"))
print(re.match("我今年\d岁了","我今年18岁了"))
print("\D")
print(re.match("\D","1"))
print(re.match("\D","a"))
print(re.match("\D","#"))
print("\W")
print(re.match("\W","1"))
print(re.match("\W","a"))
print(re.match("\W","#"))
print("\S")
print(re.match("\S",""))
print(re.match("\S"," "))
print(re.match("\S","1"))
print(re.match("\S","a"))
print("*,+,?")
print("123456")
a="123456"
print(re.match(".*",a))
print(re.match(".+",a))
print(re.match(".?",a))

你可能感兴趣的:(笔记)