Add support for compiled patterns in Filters.regex (#468)

* Add support for compiled patterns in Filters.regex and remove extra whitespaces

* Update filters.py

Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
This commit is contained in:
CyanBook 2020-08-21 07:21:53 +02:00 committed by GitHub
parent 4df9357b48
commit 3bc96b4193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,7 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import re
from typing import Callable
from typing import Callable, Union
from .filter import Filter
from ..types import Message, CallbackQuery, InlineQuery
@ -296,7 +296,7 @@ class Filters:
)
@staticmethod
def regex(pattern: str, flags: int = 0):
def regex(pattern: Union[str, re.Pattern], flags: int = 0):
"""Filter updates that match a given regular expression pattern.
Can be applied to handlers that receive one of the following updates:
@ -309,8 +309,8 @@ class Filters:
stored in the ``matches`` field of the update object itself.
Parameters:
pattern (``str``):
The regex pattern as string.
pattern (``str`` | ``Pattern``):
The regex pattern as string or as pre-compiled pattern.
flags (``int``, *optional*):
Regex flags.
@ -331,7 +331,11 @@ class Filters:
return bool(update.matches)
return create(func, "RegexFilter", p=re.compile(pattern, flags))
return create(
func,
"RegexFilter",
p=pattern if isinstance(pattern, re.Pattern) else re.compile(pattern, flags)
)
# noinspection PyPep8Naming
class user(Filter, set):