Useful in Context-Free Languages.

Basic Regular Expression Operators

OperatorMeaningExampleRecognized Strings
ConcatenationAB means A followed by Bab"ab"
Union (Alternation)A|B means A or Ba|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 timesa?"", "a"

🛠 Example:

Regular Expression:

(a|b)c*

Interpretation:

  1. "a" OR "b" followed by "c" (zero or more times).
  2. Accepts: "a", "b", "bc", "bcc", "bccc", etc.

Miscellaneous Regex

NotationMeaningExample
[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
\dDigits (0-9)\d+ matches 123, 56, 890
\wWord characters (letters, numbers, _)\w+ matches hello, A123, _name
\sWhitespace (space, tab, newline)\s+ matches spaces

Common Regular Expressions

PatternMatches
\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)