mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-23 23:39:42 +01:00
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
This commit is contained in:
parent
5c45e469d5
commit
487bce18dd
2 changed files with 18 additions and 6 deletions
|
@ -175,29 +175,40 @@ class Filters(object):
|
||||||
|
|
||||||
class regex(BaseFilter):
|
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.
|
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.
|
Refer to the documentation of the ``re`` module for more information.
|
||||||
|
|
||||||
Note: Does not allow passing groups or a groupdict like the ``RegexHandler`` yet,
|
Note:
|
||||||
but this will probably be implemented in a future update, gradually phasing out the
|
Does not allow passing groups or a groupdict like the ``RegexHandler`` yet,
|
||||||
RegexHandler (see https://github.com/python-telegram-bot/python-telegram-bot/issues/835).
|
but this will probably be implemented in a future update, gradually phasing out the
|
||||||
|
RegexHandler (See `Github Issue
|
||||||
|
<https://github.com/python-telegram-bot/python-telegram-bot/issues/835/>`_).
|
||||||
|
|
||||||
Examples:
|
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:
|
Args:
|
||||||
pattern (:obj:`str` | :obj:`Pattern`): The regex pattern.
|
pattern (:obj:`str` | :obj:`Pattern`): The regex pattern.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, 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)
|
self.name = 'Filters.regex({})'.format(self.pattern)
|
||||||
|
|
||||||
# TODO: Once the callback revamp (#1026) is done, the regex filter should be able to pass
|
# 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.
|
# the matched groups and groupdict to the context object.
|
||||||
|
|
||||||
def filter(self, message):
|
def filter(self, message):
|
||||||
|
""":obj:`Filter`: Messages that have an occurrence of ``pattern``."""
|
||||||
if message.text:
|
if message.text:
|
||||||
return bool(self.pattern.search(message.text))
|
return bool(self.pattern.search(message.text))
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -64,6 +64,7 @@ class TestFilters(object):
|
||||||
assert not Filters.regex(r'fail')(message)
|
assert not Filters.regex(r'fail')(message)
|
||||||
assert Filters.regex(r'test')(message)
|
assert Filters.regex(r'test')(message)
|
||||||
assert Filters.regex(re.compile(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'
|
message.text = 'i love python'
|
||||||
assert Filters.regex(r'.\b[lo]{2}ve python')(message)
|
assert Filters.regex(r'.\b[lo]{2}ve python')(message)
|
||||||
|
|
Loading…
Add table
Reference in a new issue