[英]Extract pattern of usernames from messages
我构建了一个 Discord py bot,它允许用户跨服务器进行通信(即两个用户不必在同一台服务器上)。 它适用于用户名简单的用户,例如littlefox#1234
或little_fox#1234
。
但是,当用户名更复杂时,如little fox#1234
之类的空格会被难住。 机器人接受诸如!hello
、 !greet
、 !bye
等命令。我尝试使用正则表达式,但这也不起作用:
import re
match = re.match(r"!\w( [a-z]*#[0-9]*)", '!hello little fox#1234')
print(match)
other_match = re.match(r"!\w( [a-z]*#[0-9]*)", '!hello little_fox#1234')
print(other_match)
然而,它不匹配任何东西。 两者都返回None
。 我该怎么办?
您可以使用
(?:!\w+\s+)?([\w\s]*#[0-9]*)
查看正则表达式演示
细节
(?:!\\w+\\s+)?
- 匹配 1 或 0 次重复的可选组
!
- 一个!
字符\\w+
- 1+ 个字字符\\s+
- 1+ 个空格([\\w\\s]*#[0-9]*)
- 第 1 组:零个或多个单词或空格字符、 #
和 0+ 数字。 请注意,如果必须至少有 1 个字母和数字,请将*
替换为+
: (?:!\\w+\\s+)?([\\w\\s]+#[0-9]+)
。
请参阅Python 演示:
import re
rx = r"(?:!\w+\s+)?([\w\s]*#[0-9]*)"
ss = ["!hello little fox#1234", "little fox#1234"]
for s in ss:
m = re.match(rx, s)
if m:
print(m.group(1)) # The username is in Group 1
对于这两个输入,输出都是little fox#1234
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.