Useful in Context-Free Languages.
Basic Regular Expression Operators
Operator | Meaning | Example | Recognized Strings |
---|---|---|---|
Concatenation | AB means A followed by B | ab | "ab" |
Union (Alternation) | A|B means A or B | a|b | "a" , "b" |
Kleene Star * | A* means 0 or more repetitions of "A" | a* | "" , "a" , "aa" , "aaa" |
Kleene Plus + | A+ means 1 or more repetitions of "A" | a+ | "a" , "aa" , "aaa" |
Optional ? or [_] | A? or [A] means "A" appears 0 or 1 times | a? | "" , "a" |
🛠 Example:
Regular Expression:
(a|b)c*
Interpretation:
"a"
OR"b"
followed by"c"
(zero or more times).- Accepts:
"a"
,"b"
,"bc"
,"bcc"
,"bccc"
, etc.
Miscellaneous Regex
Notation | Meaning | Example |
---|---|---|
[abc] | Match any single character inside | [aeiou] matches vowels |
[^abc] | Match any character except listed ones | [^0-9] matches non-digits |
[a-z] | Match any range | [A-Z] matches uppercase letters |
\d | Digits (0-9) | \d+ matches 123, 56, 890 |
\w | Word characters (letters, numbers, _) | \w+ matches hello, A123, _name |
\s | Whitespace (space, tab, newline) | \s+ matches spaces |
Common Regular Expressions
Pattern | Matches |
---|---|
\d+ | One or more digits (0-9 ) |
\w+ | One or more word characters (a-z, A-Z, 0-9, _ ) |
[A-Za-z]+ | One or more letters |
\s* | Zero or more spaces |
\d{3,5} | Between 3 and 5 digits (123 , 45678 ) |
^\w+$ | A whole word (no spaces) |