mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
🔀 Merge pull request #37
This commit is contained in:
commit
5d9f354209
@ -239,7 +239,9 @@ def command(*cmds: Union[str, Tuple[str, ...]]) -> Rule:
|
||||
def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
|
||||
"""
|
||||
:说明:
|
||||
根据正则表达式进行匹配
|
||||
根据正则表达式进行匹配。
|
||||
|
||||
可以通过 ``state["_matched"]`` 获取正则表达式匹配成功的文本。
|
||||
:参数:
|
||||
* ``regex: str``: 正则表达式
|
||||
* ``flags: Union[int, re.RegexFlag]``: 正则标志
|
||||
@ -252,8 +254,13 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
|
||||
pattern = re.compile(regex, flags)
|
||||
|
||||
async def _regex(bot: Bot, event: Event, state: dict) -> bool:
|
||||
return bool(pattern.search(str(event.message)))
|
||||
|
||||
matched = pattern.search(str(event.message))
|
||||
if matched:
|
||||
state["_matched"] = matched.group()
|
||||
return True
|
||||
else:
|
||||
state["_matched"] = None
|
||||
return False
|
||||
return Rule(_regex)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user