简体   繁体   English

regex.match找不到子字符串?

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

I have some python code where I'm trying to parse out a specific section of a string so that I can grab a list of usernames. 我有一些python代码,我试图在其中解析字符串的特定部分,以便获取用户名列表。 I think I have the regex down (I tested it online and it appears to work) but when I run my test code, it doesn't seem to actually grab what I need, as I get an error: 认为我的正则表达式已关闭(我在网上对其进行了测试,并且似乎可以正常工作),但是当我运行测试代码时,它似乎并没有真正抓住我的需要,因为出现错误:

if parsed_mod_string.group(1): 如果parsed_mod_string.group(1):
AttributeError: 'NoneType' object has no attribute 'group' AttributeError:'NoneType'对象没有属性'group'

Which I'm understanding as basically, my code isn't returning anything with my regex comparison. 我基本上了解的是,我的代码没有通过正则表达式比较返回任何内容。 My code is below, could someone help me identify where I went wrong? 我的代码在下面,有人可以帮助我确定我哪里出错了吗?

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)

Per the documentation re.match : 根据文档re.match

If zero or more characters at the beginning of string match the regular expression.. 如果字符串开头的零个或多个字符与正则表达式匹配。

That is, re.match is an "anchored" search function. 也就是说, re.match是“锚定”搜索功能。

Compare this to re.search : 将此与re.search进行比较:

Scan through string looking for the first location where [the pattern matches..] 扫描字符串以查找[模式匹配..] 的第一个位置

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

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