python中元字符及其含義如下:
| 元字符 | 含義 |
| . | 匹配除換行符以外的任意一個字符 |
| ^ | 匹配行首 |
| $ | 匹配行尾 |
| ? | 重復匹配0次或1次 |
| * | 重復匹配0次或更多次 |
| + | 重復匹配1次或更多次 |
| {n,} | 重復n次或更多次 |
| {n,m} | 重復n~m次 |
| [a-z] | 任意字符 |
| [abc] | a/b/c中的任意一個字符 |
| {n} | 重復n次 |
ref1=re.compile("[abc]{1,3}")#每次匹配a/b/c中的任意字符,一共三次
print(re.match(ref1,"caffa").group())
ref2=re.compile("[.$*+?{}|()]")
print(ref2.findall("."))#除了^和\以為其余元字符在[]均表示原意
print(ref2.findall("$"))
ref3=re.compile("[\d]")#匹配0-9數字
print(ref3.match("2").group())
ref4=re.compile("[^\d]")#匹配非數字
print(ref4.match("a").group())
ref5=re.compile(r"\\")#匹配\原始字符
print(ref5.match("\\").group())
ref6=re.compile("(a(b(c)))d")#分組
print(ref6.match("abcd").group())
print(ref6.match("abcd").group(0))
print(ref6.match("abcd").group(1))
print(ref6.match("abcd").group(2))
print(ref6.match("abcd").group(3))
