📄Regular Expression (Regex)
Also known as Regex, RegExp, Regular Expressions
A short pattern for text you want to find, like three digits and a dash.
Think of it like
Think of it like a search with blanks. Find three digits, a dash, then four digits, whatever they are.
Example
Emma needs every phone number in a long page. One regex finds all 40 of them in a second.
Why it matters
One line of regex can do the work of many lines of code. It finds or checks text.
Definition
Example code
python
import re
print(re.findall(r"\d{3}-\d{4}", "Call 555-0142 or 555-0199"))
# ['555-0142', '555-0199']Common mistake
Key takeaways
- 1A pattern, not a word. It describes a shape of text.
- 2Find, check, replace. Three common jobs.
- 3Keep it short. Long ones are hard to read.