Regex for Beginners
Regular expressions (regex) are a powerful tool for finding patterns in text. This guide explains regex basics in simple terms with examples for use in Imperium Guard triggers.
Basic Characters
Regular Characters
Regular letters and numbers match themselves:
- hello — will find the word "hello"
- 123 — will find digits "123"
Special Characters
Dot . — any character
c.t— will find "cat", "cot", "cut", "c5t"a.— will find "ah", "at", "a1"
Backslash \ — escapes special meaning
\.— will find a dot\?— will find a question mark\\— will find a backslash
Quantifiers (Repetition)
Asterisk * — 0 or more times
ha*— will find "h", "ha", "haa", "haaa"spam*— will find "spa", "spam", "spamm"
Plus + — 1 or more times
ha+— will find "ha", "haa", "haaa" (but NOT "h")o+— will find "o", "oo", "ooo"
Question Mark ? — 0 or 1 time (optional character)
hello!?— will find "hello" and "hello!"https?— will find "http" and "https"
Curly Braces {n,m} — from n to m times
\d{3}— exactly 3 digits\d{10,}— 10 or more digits (phone numbers)a{2,4}— from 2 to 4 letter "a": "aa", "aaa", "aaaa"
Character Classes
Square Brackets [...] — one of the characters
[abc]— will find "a", "b" or "c"[0-9]— any digit from 0 to 9[a-z]— any lowercase letter[a-zA-Z]— any English letter
Negation [^...] — any character EXCEPT listed
[^0-9]— any character except digits[^a-zA-Z]— any character except English letters
Predefined Classes
\d— any digit (equivalent to[0-9])\w— letter, digit or underscore\s— space, tab, newline
Alternation and Grouping
Pipe | — choice (OR)
cat|dog— will find "cat" or "dog"yes|no|maybe— will find any of the three options
Parentheses (...) — grouping
(spam)+— will find "spam", "spamspam", "spamspamspam"(ha)+— will find "ha", "haha", "hahaha"(hello|hi) world— will find "hello world" or "hi world"
Anchors (Positions)
Start ^ and End $
^hello— "hello" at the beginning of textbye$— "bye" at the end of text^word$— only "word", without other characters
Word Boundary \b
\bcat\b— will find the separate word "cat", but not "catch" or "scat"\b\d+\b— will find separate numbers
Practical Examples for Guard
Filtering Links
https?://.*
Will find any link starting with http:// or https://
How it works:
- https? — "http" and optional "s"
- :// — literally "://"
- .* — any characters until the end
Mentions and Channels
@\w+
Will find @username, @channel
How it works:
- @ — @ symbol
- \w+ — one or more letters/digits
Phone Numbers
\d{10,}
Will find a sequence of 10+ digits
How it works:
- \d — digit
- {10,} — 10 or more times
Blocking Spam Words
(spam|ad|sell|buy)
Will find any of these words
How it works:
- (...) — group
- | — choice between options
Filter Repeated Characters
(.)\1{4,}
Will find 5+ identical characters in a row (aaaaa, 11111)
How it works:
- (.) — any character (saved in a group)
- \1 — repetition of the first group (same character)
- {4,} — 4+ more times (total 5+)
Blocking E-mail
\w+@\w+\.\w+
Will find email addresses
How it works:
- \w+ — letters/digits (name)
- @ — @ symbol
- \w+ — letters/digits (domain)
- \. — dot (escaped)
- \w+ — letters/digits (zone)
Caps Lock (Uppercase)
[A-Z]{5,}
Will find 5+ uppercase letters in a row
How it works:
- [A-Z] — any uppercase letter
- {5,} — 5 or more times
Emoji Spam
[\u{1F600}-\u{1F64F}]{5,}
Will find 5+ emojis in a row
Blocking Promo Codes
\b[A-Z0-9]{5,10}\b
Will find promo codes like ABC123, PROMO2024
How it works:
- \b — word boundary
- [A-Z0-9] — uppercase letters or digits
- {5,10} — from 5 to 10 characters
- \b — word boundary
Tips for Beginners
1. Start Simple
Don't try to write a complex expression right away. Start simple and gradually make it more complex.
2. Test Before Applying
Use sites like regex101.com for testing:
1. Select JavaScript mode
2. Enable the i flag (case insensitive)
3. Paste sample text
4. Check that the pattern works correctly
3. Use Notifications for Testing
Before applying ban/mute, create a trigger with "notify" action and check if it triggers correctly.
4. Escape Special Characters
If you need to find . (dot), ? (question), * (asterisk), use \., \?, \*
5. Don't Overcomplicate
If you can use "contains" condition instead of regex — use it. Regex is needed for complex patterns.
Common Mistakes
❌ Forgot to Escape Dot
google.com
Will find "googleXcom", "google com", "google1com"
✅ Correct
google\.com
Will find only "google.com"
❌ Wrong Grouping
hello|bye everyone
Will find "hello" or "bye everyone" (not what you want)
✅ Correct
(hello|bye) everyone
Will find "hello everyone" or "bye everyone"
❌ Forgot Word Boundaries
bot
Will find "bot", "robot", "bottom"
✅ Correct
\bbot\b
Will find only "bot"
Useful Resources
- regex101.com — test regular expressions
- regexr.com — interactive tutorial with examples
- Regex Cheat Sheet
Ready-to-Use Patterns
Social Networks
(vk\.com|t\.me|instagram\.com|tiktok\.com)
Cryptocurrency
\b[13][a-km-zA-HJ-NP-Z1-9]{25,34}\b
Bitcoin addresses
IP Addresses
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Domains
\b[a-z0-9-]+\.(com|net|org|io)\b
Advertising
(subscri|promo|discount|sell|buy|@\w+)
💡 Remember: In Imperium Guard, all regex are executed with the i flag (case insensitive), so SPAM, spam and SpAm will be matched the same way.