How Do You Use Regular Expressions to Filter Telegram Messages?

Most filtering needs a word list and nothing more. Occasionally it needs a shape: a code with a fixed format, a number in a range, a line that has to begin in a particular way. That is what pattern matching is for, and it is available from the Gold plan upwards. It is also easy to get subtly wrong, so this page spends more time on the traps than on the syntax.

How a pattern is matched here

A pattern matches if it is found anywhere in the message: it is not anchored to the beginning. Writing `entry` finds it in the middle of a sentence, and if you need it at the start of a line you say so explicitly in the pattern.

Patterns are used in both lists. In the allowed list, one pattern matching is enough to relay the message; in the blocked list, one pattern matching is enough to drop it.

Trap one: a broken pattern does not fail loudly

If the expression cannot be interpreted — an unbalanced bracket, a stray quantifier — nothing is skipped and no error appears. The filter falls back to looking for that text literally, exactly as if pattern mode were off.

That behaviour is deliberate, because the alternative is a rule that stops relaying because of a typo. But it means a broken pattern can look like it is working, quietly matching almost nothing. Test with a message you know should pass before leaving it running.

Trap two: capitalisation changes your pattern, not just the text

By default the comparison ignores capitalisation, and it does that by lowering the case of both the text and the pattern. For ordinary words that is exactly right. For a pattern it can invert your meaning: an uppercase escape such as one meaning "not a digit" becomes its lowercase counterpart, which means "a digit", and a range written in capitals becomes the same range in lower case.

The rule to remember is short: if your pattern contains uppercase letters that carry meaning, switch strict capitalisation on. If it is all lower case, leave it off and enjoy the case-insensitive matching.

Patterns worth having

Anything more elaborate than these is usually a sign that the source is inconsistent, and a second, simpler rule will age better than one clever pattern.

  • A word on its own rather than inside another word, so that "buy" does not match "buyback".
  • A ticker in capitals immediately followed by a number, with strict capitalisation switched on.
  • A line that begins with a given symbol, to catch a fixed post format.
  • A number range, which a keyword list cannot express at all.

If you do not want to write patterns

You do not have to. There is a separate guide on describing the filter in plain language and having an assistant produce the pattern for you, which is the faster route for most people. And for content that is easier to judge than to describe, the AI content filter takes a criterion written in ordinary words instead of a pattern.

Which plans include it

Pattern matching in filters starts on the Gold plan; the keyword lists themselves are available from Smart. The plan comparison is on the pricing page.

Common questions

Can I use a regular expression to filter forwarded Telegram messages?

Yes, from the Gold plan upwards. Switch pattern mode on for the rule and the keywords become patterns, matched anywhere in the message rather than only as plain text.

What happens if my pattern has a syntax error?

Nothing breaks and nothing is skipped: the filter falls back to searching for that text literally. It is safe, but it means a broken pattern can look like it is working while matching almost nothing, so test it before relying on it.

Why does my pattern with uppercase escapes behave the opposite way?

Because with case-insensitive matching both the text and the pattern are lowered in case, which turns an uppercase escape into its lowercase counterpart and inverts its meaning. Switch strict capitalisation on whenever your pattern uses uppercase that carries meaning.

Is the pattern anchored to the start of the message?

No, it matches anywhere. Anchor it in the pattern itself if you need it at the beginning of a line.

Try Phantom Forwarder free

7-day trial, unlimited rules. Cloud-based, anonymous, nothing to install.

Related

More guides