From 487bce18dd2f471eab26963a650dcb943896b556 Mon Sep 17 00:00:00 2001 From: Ambro Date: Fri, 8 Feb 2019 07:12:49 -0300 Subject: [PATCH] Improve regex filter docstring and avoid compiling compiled regex (#1314) * Improve regex docstring and add test case * Add Ambro17 as contributor * rename regex filter * Fix sphinx documentation for Filters.regex * Add spacing to render Note with blue background --- telegram/ext/filters.py | 23 +++++++++++++++++------ tests/test_filters.py | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/telegram/ext/filters.py b/telegram/ext/filters.py index 856343103..03ae21a83 100644 --- a/telegram/ext/filters.py +++ b/telegram/ext/filters.py @@ -175,29 +175,40 @@ class Filters(object): class regex(BaseFilter): """ - Filters updates by searching for an occurence of ``pattern`` in the message text. + Filters updates by searching for an occurrence of ``pattern`` in the message text. The ``re.search`` function is used to determine whether an update should be filtered. + Refer to the documentation of the ``re`` module for more information. - Note: Does not allow passing groups or a groupdict like the ``RegexHandler`` yet, - but this will probably be implemented in a future update, gradually phasing out the - RegexHandler (see https://github.com/python-telegram-bot/python-telegram-bot/issues/835). + Note: + Does not allow passing groups or a groupdict like the ``RegexHandler`` yet, + but this will probably be implemented in a future update, gradually phasing out the + RegexHandler (See `Github Issue + `_). Examples: - Example ``CommandHandler("start", deep_linked_callback, Filters.regex('parameter'))`` + Use ``MessageHandler(Filters.regex(r'help'), callback)`` to capture all messages that + contain the word help. You can also use + ``MessageHandler(Filters.regex(re.compile(r'help', re.IGNORECASE), callback)`` if + you want your pattern to be case insensitive. This approach is recommended + if you need to specify flags on your pattern. + Args: pattern (:obj:`str` | :obj:`Pattern`): The regex pattern. """ def __init__(self, pattern): - self.pattern = re.compile(pattern) + if isinstance(pattern, string_types): + pattern = re.compile(pattern) + self.pattern = pattern self.name = 'Filters.regex({})'.format(self.pattern) # TODO: Once the callback revamp (#1026) is done, the regex filter should be able to pass # the matched groups and groupdict to the context object. def filter(self, message): + """:obj:`Filter`: Messages that have an occurrence of ``pattern``.""" if message.text: return bool(self.pattern.search(message.text)) return False diff --git a/tests/test_filters.py b/tests/test_filters.py index df88213cf..683cb8ad5 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -64,6 +64,7 @@ class TestFilters(object): assert not Filters.regex(r'fail')(message) assert Filters.regex(r'test')(message) assert Filters.regex(re.compile(r'test'))(message) + assert Filters.regex(re.compile(r'TEST', re.IGNORECASE))(message) message.text = 'i love python' assert Filters.regex(r'.\b[lo]{2}ve python')(message)