Regular expressions are one of those skills that pays dividends every single day — parsing log files, validating form inputs, extracting data from text, and transforming strings. This guide takes you through the core building blocks with examples you can paste directly into the DevWallah Regex Tester.
The Basics: Literals & Metacharacters
The simplest regex is a literal string. The pattern "hello" matches exactly the characters h-e-l-l-o anywhere in the test string. Metacharacters like . * + ? ^ $ { } [ ] | ( ) \ have special meaning and must be escaped with \ when you want them treated literally.
# Match a literal period: \. (note the backslash) # Match any single character: . (dot without backslash) # Match "price" followed by any digit: price\d
Character Classes
Square brackets define a character class — a set of characters any one of which can match at that position. Negate a class with ^ inside the brackets.
[aeiou] # match any vowel [^aeiou] # match any non-vowel [a-z] # match any lowercase letter [A-Z0-9] # match uppercase letters or digits \d # shorthand for [0-9] \w # shorthand for [a-zA-Z0-9_] \s # match any whitespace
Quantifiers
Quantifiers control how many times a token must appear. They apply to the preceding element — a literal, a class, or a group.
- 01? — zero or one (optional)
- 02* — zero or more
- 03+ — one or more
- 04{n} — exactly n times
- 05{n,} — n or more times
- 06{n,m} — between n and m times (inclusive)
Anchors
Anchors do not consume characters — they assert a position in the string. ^ matches the start of the string (or line in multiline mode), $ matches the end, and \b matches a word boundary.
^Hello # string must START with "Hello" world$ # string must END with "world" \bcat\b # matches "cat" but not "catch" or "concatenate"
Capture Groups & Backreferences
Wrap a sub-pattern in parentheses to create a capture group. The matched text is stored and can be referenced in a replacement string ($1, $2) or within the regex itself (\1, \2). Named groups (?<name>...) are even more readable.
# Extract year, month, day from ISO date
(\d{4})-(\d{2})-(\d{2})
# Named group version
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
# Match repeated word (backreference)
\b(\w+)\s+\1\bLookaheads & Lookbehinds
Lookaheads and lookbehinds are zero-width assertions that match a position based on surrounding context without consuming characters.
(?=...) # positive lookahead — "followed by"
(?!...) # negative lookahead — "not followed by"
(?<=...) # positive lookbehind — "preceded by"
(?<!...) # negative lookbehind — "not preceded by"
# Match a number only if followed by "px"
\d+(?=px)
# Match a price without capturing the "$"
(?<=\$)\d+\.\d{2}Real-World Examples
# Email validation (simplified)
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$
# URL validation
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b
# IPv4 address
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
# Hex color code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Paste any of these patterns into the DevWallah Regex Tester, toggle the g (global) and i (case-insensitive) flags, and watch the matches highlight in real time as you type your test string.