You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
A regular expression (regex) is a sequence of characters that defines a search pattern. Regular expressions are used for pattern matching in strings — finding, validating, and extracting text that matches a specific pattern. They are part of OCR H446 section 1.4.
| Use case | Example |
|---|---|
| Validation | Check if an email address is in the correct format |
| Searching | Find all phone numbers in a document |
| Extraction | Pull dates out of a block of text |
| Replacement | Replace all instances of "colour" with "color" |
| Lexical analysis | Identify tokens in source code (numbers, identifiers, keywords) |
| Metacharacter | Meaning | Example | Matches |
|---|---|---|---|
| . | Any single character (except newline) | a.c | "abc", "a1c", "a c" |
| * | Zero or more of the preceding element | ab*c | "ac", "abc", "abbc", "abbbc" |
| + | One or more of the preceding element | ab+c | "abc", "abbc", "abbbc" (NOT "ac") |
| ? | Zero or one of the preceding element | ab?c | "ac", "abc" (NOT "abbc") |
| ^ | Start of string | ^Hello | "Hello world" (matches), "Say Hello" (no match) |
| $ | End of string | world$ | "Hello world" (matches), "world class" (no match) |
| Alternation (OR) | cat |
Character classes match one character from a defined set:
| Syntax | Meaning | Example | Matches |
|---|---|---|---|
| [abc] | Any one of a, b, or c | [aeiou] | Any single vowel |
| [a-z] | Any character from a to z (range) | [a-z] | Any lowercase letter |
| [A-Z] | Any uppercase letter | [A-Z] | "A", "B", ..., "Z" |
| [0-9] | Any digit | [0-9] | "0", "1", ..., "9" |
| [a-zA-Z] | Any letter (upper or lower) | [a-zA-Z] | Any letter |
| [^abc] | Any character EXCEPT a, b, or c | [^0-9] | Any non-digit character |
Key Term: The ^ inside square brackets means "NOT" — it negates the character class. Outside square brackets, ^ means "start of string."
Quantifiers specify how many times a preceding element can occur:
| Quantifier | Meaning | Example | Matches |
|---|---|---|---|
| * | 0 or more | a* | "", "a", "aa", "aaa", ... |
| + | 1 or more | a+ | "a", "aa", "aaa", ... (NOT "") |
| ? | 0 or 1 | a? | "", "a" |
| {n} | Exactly n times | a{3} | "aaa" |
| {n,} | n or more times | a{2,} | "aa", "aaa", "aaaa", ... |
| {n,m} | Between n and m times | a{2,4} | "aa", "aaa", "aaaa" |
| Syntax | Meaning | Example | Matches |
|---|---|---|---|
| (abc) | Group — treats "abc" as a single unit | (ab)+ | "ab", "abab", "ababab" |
| a | b | Alternation — "a" OR "b" | cat |
| (cat | dog)s | Alternation within a group | (cat |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.