Regex Master: Pattern Creation Challenge
Regular expressions (regex) are powerful tools for pattern matching in strings. This challenge will test your ability to construct Python regex patterns to solve various text processing tasks. Mastering regex is crucial for data validation, text extraction, and search-and-replace operations.
Problem Description
You are tasked with creating Python regex patterns to match specific text patterns based on given descriptions. You will be provided with a description of the pattern to match and a set of test strings. Your goal is to write a Python function that takes a string as input and returns True if the string matches the described pattern, and False otherwise. The function should use the re module in Python.
Key Requirements:
- Your function must use the
remodule for regex operations. - The function must accurately match the described patterns.
- The function should handle edge cases gracefully (e.g., empty strings, strings with unexpected characters).
- The regex patterns should be efficient and avoid unnecessary complexity.
Expected Behavior:
The function match_pattern(text) should return True if the input text matches the specified pattern and False otherwise.
Edge Cases to Consider:
- Empty input strings.
- Strings containing characters not explicitly mentioned in the pattern description.
- Case sensitivity (unless the pattern description explicitly states case-insensitivity).
- Whitespace variations (leading, trailing, and multiple spaces) unless the pattern description specifies otherwise.
Examples
Example 1: Matching Email Addresses
Input: "test@example.com"
Output: True
Explanation: The pattern should match a standard email address format (e.g., `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`).
Input: "invalid-email"
Output: False
Explanation: This string does not conform to the email address format.
Input: "test@example"
Output: False
Explanation: Missing the top-level domain.
Example 2: Matching Dates in YYYY-MM-DD Format
Input: "2023-10-27"
Output: True
Explanation: The pattern should match dates in the format YYYY-MM-DD (e.g., `\d{4}-\d{2}-\d{2}`).
Input: "2023/10/27"
Output: False
Explanation: Incorrect date separator.
Input: "2023-10-2"
Output: False
Explanation: Incorrect day format.
Example 3: Matching Phone Numbers (US Format)
Input: "123-456-7890"
Output: True
Explanation: The pattern should match US phone numbers in the format XXX-XXX-XXXX (e.g., `\d{3}-\d{3}-\d{4}`).
Input: "(123) 456-7890"
Output: False
Explanation: Parentheses are not allowed in this specific format.
Input: "1234567890"
Output: False
Explanation: Missing the hyphens.
Constraints
- The input string
textwill consist of alphanumeric characters, spaces, and common punctuation marks. - The length of the input string
textwill be between 0 and 256 characters, inclusive. - The regex patterns should be designed to be reasonably efficient for strings of this length.
- You are not allowed to use any external libraries other than the
remodule.
Notes
- Carefully read the pattern description to understand the exact requirements.
- Consider using raw strings (e.g.,
r"\d{4}") to avoid escaping backslashes in your regex patterns. - Test your patterns thoroughly with a variety of inputs, including edge cases.
- The problem will provide the pattern description. You are responsible for translating that description into a working regex pattern.
- Focus on creating accurate and efficient patterns. Readability is a secondary concern.