Regex (Rule-Based Pattern Matching) — Email & Phone Extraction
What is Regex?
A regular expression (regex) is a sequence of characters that defines a search pattern for text matching. It lets you search for specific patterns like email formats or digit sequences instead of exact words or phrases.
Regex is not natural language understanding — it does not interpret meaning. Instead, it uses character patterns to find matches in text. It is widely used for extracting or validating structured data like emails and phone numbers.
Pattern 1 — Email Address Extraction
Here is the regex used for matching a simple email:
r"\b[\w.-]+@[\w.-]+\.\w{2,4}\b"
Explanation of parts:
- \b — Word boundary (ensures we match a complete email, not part of a bigger word)
- [\w.-]+ — One or more word characters (letters, digits, underscore), dots, or hyphens
- @ — Literal @ symbol
- [\w.-]+ — Domain part with similar allowed characters
- \. — A literal dot before the extension
- \w{2,4} — 2 to 4 word characters (common domain extensions like .com, .org, .net) :contentReference[oaicite:2]{index=2}
This pattern finds things such as john.doe@example.com in text.
Pattern 2 — Phone Number Extraction
Here is the regex used for a basic phone number matching:
r"\b\d{10,15}\b"
Explanation:
- \b — Word boundary
- \d{10,15} — 10 to 15 digits in a row (good for many international formats)
- \b — Word boundary
This pattern finds sequences of digits like 9876543210 or longer international variants.
Code Example (Python)
This shows how to use these regex patterns in Python:
import re
text = "Contact: john.doe@example.com or call 9876543210"
email_match = re.search(r"\b[\w.-]+@[\w.-]+\.\w{2,4}\b", text)
if email_match:
print("Email:", email_match.group())
phone_match = re.search(r"\b\d{10,15}\b", text)
if phone_match:
print("Phone:", phone_match.group())
Output will be:
Email: john.doe@example.com
Phone: 9876543210
Summary
- Regex lets you find patterns such as emails and phone numbers efficiently.
- It works based on predefined character rules, not language understanding.
- The two searches above are simple and widely used examples for text extraction.