【小甲鱼】【Python】正则表达式(二)

 >>> import re
#|表示或的意思
>>> re.search(r"Fish(C|D)","FishC")
<_sre.SRE_Match object; span=(0, 5), match='FishC'>
>>> re.search(r"Fish(C|D)","FishE")
>>> re.search(r"^FishC","Fish")
>>> re.search(r"^FishC","FishC dfadf")
<_sre.SRE_Match object; span=(0, 5), match='FishC'>
#^表示以什么开头
>>> re.search(r"^FishC","I FishC dfadf")
#$表示以什么结尾
>>> re.search(r"FishC$","I FishC dfadf")
>>> re.search(r"FishC$","I FishC dfadfFishC")
<_sre.SRE_Match object; span=(13, 18), match='FishC'>
#表示查找回车
>>> re.search(r"[\n]","I FishC dfadfFishC\n")
<_sre.SRE_Match object; span=(18, 19), match='\n'>
#[^...]表示查找除了...条件外的数据
>>> re.search(r"[^a-z]","I FishC dfadfFishC\n")
<_sre.SRE_Match object; span=(0, 1), match='I'>
#[^...]表示查找...条件的数据
>>> re.search(r"[a-z^]","I FishC dfadfFishC\n")
<_sre.SRE_Match object; span=(3, 4), match='i'>
>>> re.findall(r"[a-z^]","I FishC dfadfFishC\n")
['i', 's', 'h', 'd', 'f', 'a', 'd', 'f', 'i', 's', 'h']
>>> re.findall(r"[^a-z]","I FishC dfadfFishC\n")
['I', ' ', 'F', 'C', ' ', 'F', 'C', '\n']
#\1表示重复一次
>>> re.search(r"(FishC)\1","FishCFishC")
<_sre.SRE_Match object; span=(0, 10), match='FishCFishC'>
#141是八进制的a(十进制ASCII值为97)值
>>> re.search(r"(FishC)\141","FishCFishC")
>>> re.search(r"(FishC)\141","FishCFishCa")
<_sre.SRE_Match object; span=(5, 11), match='FishCa'>
>>> re.search(r"(FishC)\060","FishCFishCa")
>>> re.search(r"(FishC)\060","FishCFishC0")
<_sre.SRE_Match object; span=(5, 11), match='FishC0'>
>>> re.search(r"\.","FishCFishC0.com")
<_sre.SRE_Match object; span=(11, 12), match='.'>
>>> re.search(r"[.]","FishCFishC0.com")
<_sre.SRE_Match object; span=(11, 12), match='.'>
>>> re.search(r"C*","FishCFishC0.com")
<_sre.SRE_Match object; span=(0, 0), match=''>
#*表示重复无限次
>>> re.search(r"(FishC)*","FishCFishC0.com")
<_sre.SRE_Match object; span=(0, 10), match='FishCFishC'>
#+表示重复1到无限次
>>> re.search(r"(FishC)+","FishCFishC0.com")
<_sre.SRE_Match object; span=(0, 10), match='FishCFishC'>
#?表示重复0或1次
>>> re.search(r"(FishC)?","FishCFishC0.com")
<_sre.SRE_Match object; span=(0, 5), match='FishC'>
>>> s="<html><title>I love FishC.com</title></html>"
>>> re.search(r"<.+>",s)
<_sre.SRE_Match object; span=(0, 44), match='<html><title>I love FishC.com</title></html>'>
>>> re.search(r"<.+?>",s)
<_sre.SRE_Match object; span=(0, 6), match='<html>'>
上一篇:C++ DLL注册


下一篇:X86控制寄存器和系统地址寄存器