繁体   English   中英

regex.match找不到子字符串?

[英]regex.match isn't finding the substring?

我有一些python代码,我试图在其中解析字符串的特定部分,以便获取用户名列表。 认为我的正则表达式已关闭(我在网上对其进行了测试,并且似乎可以正常工作),但是当我运行测试代码时,它似乎并没有真正抓住我的需要,因为出现错误:

如果parsed_mod_string.group(1):
AttributeError:'NoneType'对象没有属性'group'

我基本上了解的是,我的代码没有通过正则表达式比较返回任何内容。 我的代码在下面,有人可以帮助我确定我哪里出错了吗?

import re
MOD_LIST_MSG = re.compile(r":\w+\.\w+\.\w+ NOTICE #\w+ :The moderators of this room are:(.*)")

test_msg = ":tmi.twitch.tv CAP * ACK :twitch.tv/commands\r\n:tmi.twitch.tv NOTICE #secondubly :The moderators of this room are: mod1, mod2, mod3, mod4\r\n"

parsed_mod_string = MOD_LIST_MSG.match(test_msg)
if parsed_mod_string.group(1):
    mod_list = parsed_mod_string.group(1).split(', ')
    print(mod_list)

根据文档re.match

如果字符串开头的零个或多个字符与正则表达式匹配。

也就是说, re.match是“锚定”搜索功能。

将此与re.search进行比较:

扫描字符串以查找[模式匹配..] 的第一个位置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM