From 59a8bd8c975bec485e1c4f4c81d07e2f0c6be561 Mon Sep 17 00:00:00 2001 From: rkroom <53672435+rkroom@users.noreply.github.com> Date: Mon, 26 Oct 2020 17:18:26 +0800 Subject: [PATCH 1/3] add matched_string when rule regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当使用正则匹配消息成功时,向state添加matched_string以保存匹配到的内容供接下来使用。 --- nonebot/rule.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nonebot/rule.py b/nonebot/rule.py index b1927e59..fed21d70 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -247,8 +247,12 @@ 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_string = pattern.search(str(event.message)) + if matched_string: + state["matched_string"] = matched_string.group() + return True + else: + return False return Rule(_regex) From 47f491039c73c75af24fd17b400ad14ad4075528 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Wed, 28 Oct 2020 13:23:48 +0800 Subject: [PATCH 2/3] :bulb: update regex docstring --- nonebot/rule.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/nonebot/rule.py b/nonebot/rule.py index fed21d70..6694c2b2 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -234,7 +234,9 @@ def command(command: Tuple[str, ...]) -> Rule: def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule: """ :说明: - 根据正则表达式进行匹配 + 根据正则表达式进行匹配。 + + 可以通过 ``state["matched"]`` 获取正则表达式匹配成功的文本。 :参数: * ``regex: str``: 正则表达式 * ``flags: Union[int, re.RegexFlag]``: 正则标志 @@ -247,9 +249,9 @@ 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: - matched_string = pattern.search(str(event.message)) - if matched_string: - state["matched_string"] = matched_string.group() + matched = pattern.search(str(event.message)) + if matched: + state["matched"] = matched.group() return True else: return False From c3bd8ebf57d73dc91d5658bfd45c5b75dd7fe010 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Wed, 28 Oct 2020 13:45:54 +0800 Subject: [PATCH 3/3] :construction: change result store --- nonebot/rule.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nonebot/rule.py b/nonebot/rule.py index 6694c2b2..4bb4063d 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -236,7 +236,7 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule: :说明: 根据正则表达式进行匹配。 - 可以通过 ``state["matched"]`` 获取正则表达式匹配成功的文本。 + 可以通过 ``state["_matched"]`` 获取正则表达式匹配成功的文本。 :参数: * ``regex: str``: 正则表达式 * ``flags: Union[int, re.RegexFlag]``: 正则标志 @@ -251,9 +251,10 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule: async def _regex(bot: Bot, event: Event, state: dict) -> bool: matched = pattern.search(str(event.message)) if matched: - state["matched"] = matched.group() + state["_matched"] = matched.group() return True else: + state["_matched"] = None return False return Rule(_regex)