mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 04:56:57 +08:00
add matched_string when rule regex
当使用正则匹配消息成功时,向state添加matched_string以保存匹配到的内容供接下来使用。
This commit is contained in:
parent
c85e50d73d
commit
59a8bd8c97
@ -247,8 +247,12 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
|
|||||||
pattern = re.compile(regex, flags)
|
pattern = re.compile(regex, flags)
|
||||||
|
|
||||||
async def _regex(bot: Bot, event: Event, state: dict) -> bool:
|
async def _regex(bot: Bot, event: Event, state: dict) -> bool:
|
||||||
return bool(pattern.search(str(event.message)))
|
matched_string = pattern.search(str(event.message))
|
||||||
|
if matched_string:
|
||||||
|
state["matched_string"] = matched_string.group()
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
return Rule(_regex)
|
return Rule(_regex)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user