-
I want to create regexp filter for message handler but only if message is exactly 2 words? Example message: |
Beta Was this translation helpful? Give feedback.
Answered by
evgfilim1
May 8, 2021
Replies: 1 comment 3 replies
-
Hello, @100nandoo. You can use this regexp for your handler: Explanation
Example@dp.message_handler(regexp=r"^(\w+) (\w+)$")
async def words(message: types.Message, match: typing.Match[str]):
await message.reply(f"Convert {match[1]} to {match[2]}") # Note that capturing groups are counted from 1 as 0 is reserved for full match In your case it would reply "Convert BTC to USDT" See also
|
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Olegt0rr
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, @100nandoo. You can use this regexp for your handler:
^(\w+) (\w+)$
Explanation
^
asserts the start of the line(...)
starts capturing group, so you can use the result of this capture later\w
is the same as[a-zA-Z0-9_]
[...]
is a choice list, so it matches exactly one character from this list[a-z]
matches everything froma
(ASCII codepoint 97) toz
(ASCII codepoint 122)[a-z0-9]
helps you to match either[a-z]
or[0-9]
[a-zA-Z0-9_]
is one of[a-z]
,[A-Z]
,[0-9]
or_
.+
is the same as{1,}
{a,b}
means "match previous expression (\w
in this case) froma
tob
times"a
is missing, it is considered equal 0b
is missing, it is considered maximum