Documentation menu

Replacements & Regex

Text Replacements & Regular Expressions

Ordered find and replace pairs, literal or regular expression, applied in one deterministic pass with Telegram entity offsets preserved.

Verified against the checkout built for v2.2.4 · updated 20 Oct 2026

Overview

The Replacement Engine enables fine-grained textual substitutions on incoming Telegram posts. This allows modifying brand names, re-tagging topics, or translating recurring phrases before broadcasting to your destination chats.

Dictionary-Based Replacements

Define exact key-value pairs to replace across the message text and media captions:

# Example Dictionary Pairings
"Binance" -> "Crypto Exchange"
"Discount Code: ABC" -> "Discount Code: PROMO2026"
"Follow @OldChannel" -> "Follow @OurChannel"

Regex Transformations

For dynamic patterns, AutoTelegramForward supports standard Python regex expressions:

  • Pattern Matching: (?i)price:\s*\$([0-9.]+)
  • Group Substitution: Valuation: $$1 USD

Catastrophic Backtracking (ReDoS) Defense

User-supplied regular expressions can inadvertently cause CPU exhaustion (ReDoS). AutoTelegramForward validates every pattern against strict compilation limits and enforces an execution timeout on every regex substitution. If a pattern times out, it logs an error and gracefully preserves the original text without dropping the message.

Telegram Entity Preservation

When performing textual modifications, naive string replacement breaks Telegram formatting entities (such as MessageEntityBold, MessageEntityItalic, or MessageEntityCode) because byte offsets shift. AutoTelegramForward re-calculates entity offsets or reconstructs formatted text in MarkdownV2/HTML mode to ensure typography remains crisp and uncorrupted.