[英]Extract pattern of usernames from messages
I built a Discord py bot that allows users to communicate across servers (ie both users don't have to be on the same server).我构建了一个 Discord py bot,它允许用户跨服务器进行通信(即两个用户不必在同一台服务器上)。 It works for those with simple usernames such as
littlefox#1234
or little_fox#1234
.它适用于用户名简单的用户,例如
littlefox#1234
或little_fox#1234
。
However, when the username is more complex with spaces such as little fox#1234
it gets stumped.但是,当用户名更复杂时,如
little fox#1234
之类的空格会被难住。 The bot accepts commands such as !hello
, !greet
, !bye
etc. I tried using regex but that doesn't work either:机器人接受诸如
!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)
However it does not match anything.然而,它不匹配任何东西。 Both return
None
.两者都返回
None
。 What do I do?我该怎么办?
You may use您可以使用
(?:!\w+\s+)?([\w\s]*#[0-9]*)
See the regex demo查看正则表达式演示
Details细节
(?:!\\w+\\s+)?
- an optional group matching 1 or 0 repetitions of !
- a !
!
char\\w+
- 1+ word chars \\w+
- 1+ 个字字符\\s+
- 1+ whitespaces \\s+
- 1+ 个空格([\\w\\s]*#[0-9]*)
- Group 1: zero or more word or whitespace chars, #
and 0+ digits. ([\\w\\s]*#[0-9]*)
- 第 1 组:零个或多个单词或空格字符、 #
和 0+ 数字。 Note that in case there must be at least 1 letter and digit replace *
with +
: (?:!\\w+\\s+)?([\\w\\s]+#[0-9]+)
.请注意,如果必须至少有 1 个字母和数字,请将
*
替换为+
: (?:!\\w+\\s+)?([\\w\\s]+#[0-9]+)
。
See the Python demo :请参阅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
For both inputs, the output is little fox#1234
.对于这两个输入,输出都是
little fox#1234
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.