我将从&#34开始;我不熟悉Python"。我尝试为我的频道更改扭曲的默认ircbot脚本,但我无法让re.match
或re.search
正常工作。
例如,这有效:
prompt = ("%s" % self.nickname)
prompt1 = ("%s:" % self.nickname)
prompt2 = ("%s," % self.nickname)
if msg == (prompt1 + " whoareyou?") or msg == (prompt2 + " who are you?") or msg == (prompt1 + " whoareyou") or msg == (prompt2 + " who are you"):
这个也是
if msg == (prompt1 + " help") or msg == (prompt2 + " help"):
但是这两个条件在机器人上不起作用(但是它们在本地脚本上工作):
if re.search(r'%s[:,] help' % self.nickname, msg):
elif re.search(r'%s[:,] who ?are ?you?' % self.nickname, msg):
可以找到以前版本的未使用re.search
的脚本here
答案 0 :(得分:0)
我找到了导致问题的原因。 Twisted包含一个逻辑来处理机器人的昵称冲突。它在名称的末尾添加^
:
# For fun, override the method that determines how a nickname is changed on
# collisions. The default method appends an underscore.
def alterCollidedNick(self, nickname):
"""
Generate an altered version of a nickname that caused a collision in an
effort to create an unused related name for subsequent registration.
"""
return nickname + '^'
这导致我的机器人昵称以nickname^
结尾,并且因为我使用%s
添加昵称变量作为正则表达式的一部分,它将^
解释为修饰符。
if re.search(r'%s[:,] help' % self.nickname, msg):
对此:
if re.search(r'nickname^[:,] help', msg):