What is regex?
A regular expression (regex) is a pattern that describes text. Instead of searching for one exact word, you describe the shape of what you want: \d+ means "one or more digits", [A-Z][a-z]+ means "a capital letter followed by small letters", and ^\w+@\w+\.\w+$ means "something that looks like an email address". Developers use regex to check emails and phone numbers, find and replace text, and pull values out of logs and forms. The tester lets you try a pattern and see exactly what it matches, before you use it in code. Try it in our Regex Tester — type a pattern, paste some text, and watch the matches highlight instantly.
The basic building blocks
Most characters in a regex match themselves: the pattern "cat" matches the word cat. The power comes from the special characters. A dot . matches any single character. Square brackets [ ] match one character from a set: [abc] matches a, b or c, and [0-9] matches any digit. A backslash \ gives a character a special meaning, or takes the special meaning away: \. matches a real full stop, not "any character". Learn these three — dot, brackets, backslash — and you can already write useful patterns.
Character classes and shortcuts
Shortcuts save typing: \d means any digit (0–9), \w means any word character (letters, digits and underscore), and \s means any whitespace (space, tab, newline). Their capitals are the opposites: \D is a non-digit, \W is a non-word character, \S is a non-space. So \d+ finds numbers, \w+ finds words, and \s+ finds the gaps between them. A Pakistani mobile number like 0300-1234567 can be matched with 03\d{2}-\d{7} — "03", then two digits, a dash, then seven digits.
Quantifiers: how many?
Quantifiers say how many times the previous thing may appear. A + means one or more, * means zero or more, and ? means zero or one (optional). Curly braces give exact counts: \d{4} means exactly four digits, \d{2,4} means two to four, and \d{3,} means three or more. So a year is \d{4}, a CNIC is \d{5}-\d{7}-\d, and a word is \w+. Be careful with * — it can match nothing, which sometimes gives surprising empty matches.
Anchors and boundaries
Anchors tie a pattern to a position. ^ means the start of the text (or the start of a line with the m flag), and $ means the end. So ^\d+$ matches a text that is entirely digits, while \d+ alone matches digits anywhere. The word boundary \b matches the edge of a word: \bcat\b matches "cat" but not "catalogue" or "educate". Anchors are what turn a loose search into a validation: an email pattern without ^ and $ will match an email inside a longer text, but with them it checks that the whole text is an email.
Capture groups and replacements
Round brackets ( ) create capture groups: the part of the match inside the brackets is saved, and you can reuse it. In (\w+)@(\w+), group 1 is the name before the @ and group 2 is the domain after it. In a replacement, $1 means "the first group" and $2 means "the second": the pattern (\d{4})-(\d{2})-(\d{2}) replaced with $3/$2/$1 turns 2026-11-17 into 17/11/2026. Groups can also be nested, and (?:...) makes a group that does not capture — useful when you need brackets for grouping but not for reuse.
Ready-made patterns
The tester includes ready-made patterns you can tap and try: Email, URL, Pakistani mobile number, CNIC, date, and more. Use them as starting points — copy one, change it for your own case, and test it against real examples, including the ones that should not match. A pattern that matches everything is as useless as one that matches nothing. Always test both sides: five examples that should match, and five that should not.