In this lesson, we will cover the fundamental building blocks and common patterns.
Basic Elements:
Literal Characters:
A regular expression can consist of literal characters that match exactly. For example, the regex `python` will match the string "python" in a text.
Character Classes:
Character classes are enclosed in square brackets ([]) and allow you to match any one of the characters within the brackets. For instance, `[aeiou]` matches any vowel.
Quantifiers:
Quantifiers specify how many times a character or group should appear. The asterisk `*` means zero or more occurrences, the plus `+` means one or more occurrences, and the question mark `? ` means zero or one occurrence.
Wildcards:
The dot (`.`) serves as a wildcard, matching any character except a newline. For example, `b.t` would match "bat," "bit," "but," etc.
Examples:
Literal Characters:
Regex: `cat`
Matches: "cat" in "The cat is black."
Character Classes:
Regex: `[aeiou]`
Matches: Any vowel in "apple" or "orange."
Quantifiers:
Regex: `lo+l`
Matches: "lool" or "loooool" in "The cat is looking."
Wildcards:
Regex: `b.t`
Matches: "bat," "bit," "but," etc., in "The bat is in the box."
Exercise: Write Simple Regular Expressions to Match Specific Patterns
Match Dates:
Write a regex pattern to match dates in the format "YYYY-MM-DD." Test it against strings like "2022-01-01" and "2023-12-31."
Match Email Addresses:
Create a regex pattern to match basic email addresses. Test it against strings like "user@example.com" and "john_doe123@gmail.com."
Match Phone Numbers:
Develop a regex pattern to match standard U.S. phone numbers in the format "(XXX) XXX-XXXX." Test it against numbers like "(555) 123-4567."
Match Hexadecimal Colors:
Construct a regex pattern to match hexadecimal color codes, such as "#RRGGBB." Test it against strings like "#1a2b3c" and "#ff9900."
Remember to use online regex testers or Python's re module to validate and test your patterns against different strings. Practice refining your patterns based on the results and experiment with variations of the exercises.