首页 > 代码库 > Python正则

Python正则

import restr4 = r"^http://qy.chinahr.com/cvm/preview\?cvid=\w{24,25}&from=sou&gtid=\w{8}-(\w{4}-){3}\w{12}$"s = re.match(str4, "http://qy.chinahr.com/cvm/preview?cvid=5799bb78e4b036ea3badaa64&from=sou&gtid=3aea5f99-6797-48bc-8b62-767a16d748c1")print(s, type(s))if str(s) == ‘None‘:    print(1)else:    print(2)

Python正则写法

1)导入re

2)正则字符串,注意特殊字符转义

3)re.match(正则字符串,待匹配字符串) 

4)打印匹配结果,如下

<_sre.SRE_Match object; span=(0, 114), match=‘http://qy.chinahr.com/cvm/preview?cvid=5799bb78e4> <class ‘_sre.SRE_Match‘>2Process finished with exit code 0

  

Python正则