首页 > 解决方案 > 有没有办法找到特定字符的跨度,然后是任意数量的特定字符?

问题描述

具体来说,我在一个不和谐的机器人中使用它,但这在这里并不(不应该)重要。我目前正在使用re.search来查看字符串中是否存在一个字符(比如'x'),然后是任意数量的第二个字符(比如'y')并返回它的跨度。

当前代码:

if 'x' in message.content.lower(): # message.content is a string and is changed to all lowercase
    match = re.search('xyyy', message.content.lower()).span() # currently only finds 'x' followed by exactly three 'y's
    print(match) # prints the span of 'xyyy' as a tuple; such as (0,4)
    print(f'{message.content[match[0]:match[1]]}y') # prints the span of the message + 'y'
# both these print statements are 'await message.send()'s instead of prints

然而,这个设置的问题在于它只查找 x 后跟 3 ys。我只是错过了一些明显的东西还是有更好的模块呢?

标签: pythonre

解决方案


推荐阅读