如何在Python中查找字符串中的x
我想知道如何在字符串中找到您不知道是什么的字符串。我正在编写一个 IRC 机器人,我需要这个功能。我希望能够写:
!问候格雷格
,然后我的机器人应该说“嗨,格雷格!”。所以问候之后的内容是可变的。如果我写!问候马修,它会说“嗨,马修!”。 这可能吗?
多谢。
安德赛
I was wondering how to go about finding a string you don't know what is, in a string. I am writing an IRC bot and i need this function. I want to be able to write:
!greet Greg
and then my bot is supposed to say "Hi, Greg!". So what comes after greet is variable. And if i wrote !greet Matthew it would say "Hi, Matthew!".
Is this possible?
Thanks a lot.
Andesay
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您计划为您的机器人添加更多复杂性,我建议使用如下正则表达式:
首先定义您的机器人可能需要的功能。
然后定义模式和命令字典:
现在您只需使用用户输入和适当的函数调用
pattern.match()
:如果用户输入无效命令,则会引发 TypeError 。
现在,您只需编辑
commands
-dict 即可添加任何功能。If you plan on adding more complexity to your bot, i would suggest using regular expressions like this:
At first you define the functions your bot may need.
Then you define the pattern and a dict of commands:
Now you just have to call
pattern.match()
with the user input and the appropriate function:If a user enters an invalid command, a TypeError is thrown.
Now you can add any function just by editing the
commands
-dict.很简单:
It's simple:
如果问候语中是“Greg”:
doSomething("Hi Greg")
关键是字符串采用 in 运算符
if "Greg" in greet:
doSomething("Hi Greg")
the key is that strings take the in operator