From e5de670d4092570aea458190879f238fae5c3d42 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 25 Apr 2018 09:33:44 +0200 Subject: [PATCH] Allow Filters.regex to pass all matches (as list) This is done by using the new "matches" field in the Message type --- pyrogram/client/filters/filters.py | 14 +++++++++----- pyrogram/client/types/message.py | 10 ++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 3386c352..1c16bf32 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -147,15 +147,19 @@ class Filters: Args: pattern (``str``): - The RegEx pattern. + The RegEx pattern as string, it will be applied to the text of a message. When a pattern matches, + all the `Match Objects `_ + are stored in the *matches* field of the :class:`Message ` itself. flags (``int``, optional): RegEx flags. """ - return build( - "Regex", lambda _, m: bool(_.p.search(m.text or "")), - p=re.compile(pattern, flags) - ) + + def f(_, m): + m.matches = [i for i in _.p.finditer(m.text or "")] + return bool(m.matches) + + return build("Regex", f, p=re.compile(pattern, flags)) @staticmethod def user(user: int or str or list): diff --git a/pyrogram/client/types/message.py b/pyrogram/client/types/message.py index 79ed5cc7..55c3ce05 100644 --- a/pyrogram/client/types/message.py +++ b/pyrogram/client/types/message.py @@ -174,6 +174,10 @@ class Message(Object): via_bot (:obj:`User `): Via bot. + + matches (``list``): + A list containing all `Match Objects `_ that match + the text of this message. Only applicable when using :obj:`Filters.regex `. """ ID = 0xb0700003 @@ -181,7 +185,7 @@ class Message(Object): self, message_id: int, date: int = None, - chat = None, + chat=None, from_user=None, forward_from=None, forward_from_chat=None, @@ -222,7 +226,8 @@ class Message(Object): successful_payment=None, connected_website=None, views: int = None, - via_bot=None + via_bot=None, + matches: list = None ): self.message_id = message_id # int self.date = date # int @@ -268,3 +273,4 @@ class Message(Object): self.connected_website = connected_website # flags.38?string self.views = views # flags.39?int self.via_bot = via_bot # flags.40?User + self.matches = matches