Learn regular expressions by seeing patterns, test cases, and match results side-by-side
Extract and organize matched data using capturing groups
Parentheses () create capturing groups that extract matched portions.
Pattern breakdown:
(\d{3}) - captures exactly 3 digits (Group 1)- - literal hyphen(\d{4}) - captures exactly 4 digits (Group 2)This extracts phone numbers and separates the prefix and number into groups.
Source: Python Regex HOWTO - Grouping
Call me at 555-1234 Another number: 867-5309 Invalid: 12-345
Named groups (?P<name>...) let you reference captures by name instead of number.
Pattern breakdown:
(?P<year>\d{4}) - 4 digits named "year"(?P<month>\d{2}) - 2 digits named "month"(?P<day>\d{2}) - 2 digits named "day"This makes extracted data much more readable and maintainable in code.
Source: Python re Syntax - Named Groups
Today is 2024-01-15 Another date: 2023-12-25 Invalid: 24-1-5
Non-capturing groups (?:...) group patterns without creating a capture.
Pattern breakdown:
(?:Mr|Ms|Mrs) - match title but don't capture it\. - literal period(\w+) - capture the last name (Group 1)Use non-capturing groups when you need grouping for alternation or quantifiers but don't need to extract that part.
Mr. Smith Ms. Johnson Mrs. Williams Dr. Brown