patternreplace with

Regex replacement for Telegram forwarding

Clean up forwarded posts with a pattern and a “Replace with”: remove another channel’s signature, mentions, links, hashtags or ad lines, or rewrite text the way you want.

How patterns work

  • A pattern runs on the whole text of the post, across bold text, links and quotes, so one pattern removes a signature like “🔴 @channel | News” even when its parts are formatted differently.
  • Letter case is ignored and matching works line by line: ^ is the start of a line and $ its end. Put (?-i) at the start of a pattern to match the exact case.
  • “Replace with” can reuse what was found: $1 inserts the first group in parentheses, $& the whole match. Leave it empty to delete the match.
  • Patterns run in order, after “Remove lines containing” and before word replacements. Your own header and footer are added afterwards and are never changed.
  • Formatting stays: words you change inside bold text stay bold. A quote, bold text or link disappears only when all of its text is removed.
  • Each pattern counts as one replacement of the rule: 1 per rule on Premium, 5 per rule on Elite.

Add a pattern in the mini app

  1. 1

    Open your rule

    In the mini app, go to Forwarding and open the rule whose posts you want to clean, or create a new one.

  2. 2

    Find “Patterns (regex)”

    Open “Link & word replacement” and scroll to “Patterns (regex)”. Tap a preset such as “Channel’s signature”, or type your own pattern.

  3. 3

    Check the preview

    Paste a real post from the source into “Try it on a post”. The result shows exactly what will be delivered: the preview uses the same engine as delivery.

  4. 4

    Add and save

    Tap + to add the pattern, then save the rule. It applies to every new post and to edits of posts already delivered.

Ready-made recipes

Copy a pattern into the app and change the example names, like channel, to yours. Every example below is the real output of the delivery engine.

Remove a channel’s signature line, with its emoji

Pattern
\p{So}*\s*@channel\b.*$
Replace with
(empty)
Before → after
Markets open higher today. 🔴 @channel | NewsMarkets open higher today.

Replace the source’s @mention with yours

Pattern
@sourcechannel\b
Replace with
@mychannel
Before → after
Read more in @sourcechannelRead more in @mychannel

Remove every @mention

Pattern
\s*@[A-Za-z0-9_]{4,}
Replace with
(empty)
Before → after
Thanks @someone and @another for the tipThanks and for the tip

Remove links

Pattern
\s*(?:https?://|t\.me/)\S+
Replace with
(empty)
Before → after
Full story: https://example.com/news/42Full story:

Remove hashtags, in any language

Pattern
\s*#[\p{L}\p{N}_]+
Replace with
(empty)
Before → after
Bitcoin hits a new high #crypto #btcBitcoin hits a new high

Remove lines that start with “Join” or “Subscribe”

Pattern
^(?:join|subscribe)\b.*$
Replace with
(empty)
Before → after
Rates are unchanged. Join us for more news!Rates are unchanged.

Remove “via @bot” credits

Pattern
\s*via @[A-Za-z0-9_]+
Replace with
(empty)
Before → after
Rates are unchanged via @newsbotRates are unchanged

Remove everything after a divider (ads at the end)

Pattern
(?s)———.*
Replace with
(empty)
Before → after
Rates are unchanged. ——— Ads: buy now at shop.exampleRates are unchanged.

Remove lines made only of emoji

Pattern
^[\p{So}\s]+$
Replace with
(empty)
Before → after
🔥🔥🔥 Rates are unchanged.Rates are unchanged.

Rewrite prices: 100 USD → $100

Pattern
(\d+)\s*USD
Replace with
$$$1
Before → after
Now only 100 USDNow only $100

Replace a word only in an exact letter case

Pattern
(?-i)\bBTC\b
Replace with
Bitcoin
Before → after
BTC is up, btc fans cheerBitcoin is up, btc fans cheer

Rewrite a “Source:” line

Pattern
^source:\s*(.+)$
Replace with
via $1
Before → after
Rates are unchanged. Source: ReutersRates are unchanged. via Reuters

Pattern cheat sheet

Patterns use the RE2 syntax, which works like regular expressions in most programming languages.

.any character except a new line
\da digit, 0 to 9
\wa Latin letter, digit or _
\p{L}a letter in any language
\p{So}an emoji or other symbol
\sa space, tab or new line
[abc]one of the characters a, b or c
[^abc]any character except a, b and c
*the previous item, any number of times (also none)
+the previous item, one or more times
?the previous item is optional
{2,5}the previous item, 2 to 5 times
^ $the start and the end of a line
\bthe edge of a Latin word
( )a group: its text can be reused as $1, $2…
(?: )a group that is not numbered
a|beither a or b
\.a real dot: put \ before . * + ? ( ) [ ] { } | ^ $ to match them as they are
(?-i)from here, letter case must match exactly
(?s)from here, . also matches new lines

In “Replace with”

$1 … $9the text of group 1 to 9
$&the whole match
$$a dollar sign
(empty)the match is deleted

Good to know

  • A pattern can be up to 200 characters long.
  • Lookarounds like (?=…) and backreferences like \1 are not supported. RE2 leaves them out so that every pattern runs in time proportional to the post: no pattern can slow delivery down.
  • A pattern that also matches empty text, like \s* or ^ on its own, is refused: it would insert “Replace with” between every character.
  • Patterns change text, not formatting: a quote stays a quote unless all of its text is removed.
  • Link addresses are never changed; only the visible text of a link can be.
  • If nothing is left of a text post, it is not sent. Media posts are still sent, without the caption text you removed.

Questions

My pattern doesn’t match. Why?

Paste the real post into “Try it on a post”. Common causes: \w and \b only cover Latin letters, so use \p{L} for words in other alphabets; characters like . | ( ) need a \ in front; ^ and $ mean a line, not the whole post.

Can a pattern break a post?

It can remove more than you meant, so check the preview first. It can’t break links or formatting: link addresses stay as they are, and the formatting is kept around the new text.

Which plans include patterns?

Premium (1 replacement per rule) and Elite (5 per rule). A pattern, a word replacement and a removed line each count as one.

Is it fast enough for busy channels?

Yes. Patterns run on RE2, whose matching time grows only with the length of the post, so no pattern can hold up delivery.

Clean up your forwarded posts

Open the mini app, pick a rule and add your first pattern.

Start in Telegram