使用捕获组时,re.findall()会产生奇怪的结果

时间:2020-02-28 12:23:01

标签: python regex python-3.8

我正在使用Python 3.8进行一些文本分析,并做了以下正则表达式:

regex = r'((tgda|teleg|lic|sh[uo]|b[ou][sn]|imo|inst|what|vats|nom).+((yoz)|(bor))+)|@|pong'
regex_compiled = re.compile(regex, re.MULTILINE | re.IGNORECASE)

text = "ping me at @user1 and @user2"

r = re.findall(regex_compiled, text)
m = re.search(regex_compiled, text)
print(r)
print(m)

要检查我的正则表达式,我在Python模式(example)中使用了多个在线解析器,但是内置的re模块返回了奇怪的东西:

  • re.findall()返回一个列表,其中包含2个元组,内部有空字符串。
  • re.search()成功找到“ @”符号的第一次出现(尽管我需要找到所有出现的地方)

输出:

[('', '', '', '', ''), ('', '', '', '', '')]  # <-- this is what findall() returns (strange)
<re.Match object; span=(11, 12), match='@'>   # <-- this is what search() returns (correct)

请您说明一下我在这种情况下做错了什么吗?

0 个答案:

没有答案
相关问题