首页 > Python:正则匹配

Python:正则匹配

代码如下:

content = "\">123|abc"
pattern = re.compile(r'>(?P<username>[a-zA-Z0-9]+)\|(?P<password>[a-zA-Z0-9]+)',re.I|re.S) 
match = pattern.match(content)        
username = match.group("username")
password = match.group("password")

为何以上的正则表达式,没法匹配到数据呢?
如果正则改成

.*?>(?P<username>[a-zA-Z0-9]+)\|(?P<password>[a-zA-Z0-9]+)

这样就可以了,但是这样容易误报


这个真的要说“好好去看文档(RTFM)”了……

RegexObject.match()要求匹配内容出现在字符串的开头(beginning).search()才是任意位置。

【热门文章】
【热门文章】