Valid Phone Numbers - Problem
Phone Number Validator - Your task is to process a text file containing phone numbers and identify which ones follow valid formatting patterns.
You're given a file
โข
โข
Where
Goal: Write a one-liner bash command that prints only the valid phone numbers from the file.
Note: Each line is guaranteed to have no leading or trailing whitespace, so you only need to focus on pattern matching.
You're given a file
file.txt where each line contains a potential phone number. A valid phone number must match one of these exact formats:โข
(xxx) xxx-xxxx - Area code in parentheses followed by space and seven digits with dashโข
xxx-xxx-xxxx - Ten digits separated by dashesWhere
x represents any digit from 0-9.Goal: Write a one-liner bash command that prints only the valid phone numbers from the file.
Note: Each line is guaranteed to have no leading or trailing whitespace, so you only need to focus on pattern matching.
Input & Output
example_1.txt โ Basic Valid Numbers
$
Input:
987-123-4567
123 456 7890
(555) 123-4567
โบ
Output:
987-123-4567
(555) 123-4567
๐ก Note:
The first and third lines match valid formats (xxx-xxx-xxxx and (xxx) xxx-xxxx respectively). The second line has spaces instead of dashes, so it's invalid.
example_2.txt โ Mixed Valid and Invalid
$
Input:
(123) 456-7890
123-456-78901
123-456-7890
(123)456-7890
โบ
Output:
(123) 456-7890
123-456-7890
๐ก Note:
First line: valid parentheses format. Second line: too many digits (11 total). Third line: valid dash format. Fourth line: missing space after parentheses.
example_3.txt โ Edge Cases
$
Input:
000-000-0000
(000) 000-0000
123-abc-4567
(123) 456-789a
โบ
Output:
000-000-0000
(000) 000-0000
๐ก Note:
Leading zeros are allowed since they're still digits. However, letters (abc, a) make the numbers invalid even if the format structure is correct.
Visualization
Tap to expand
Understanding the Visualization
1
Pattern Template
Create regex pattern that matches both valid formats: (xxx) xxx-xxxx OR xxx-xxx-xxxx
2
Line-by-Line Matching
Apply pattern to each line in the file using grep
3
Instant Validation
Regex engine validates entire format in single operation
4
Output Results
Print only lines that match the pattern exactly
Key Takeaway
๐ฏ Key Insight: Regular expressions provide a powerful pattern matching template that can validate complex formats in a single operation, making them perfect for one-liner solutions like this phone number validator.
Time & Space Complexity
Time Complexity
O(n*m)
n lines in file, m characters per line (constant ~14), so effectively O(n)
โ Linear Growth
Space Complexity
O(1)
Only using variables for current line processing
โ Linear Space
Constraints
- Each line contains exactly one potential phone number
- Lines have no leading or trailing whitespace
- Valid formats are exactly (xxx) xxx-xxxx or xxx-xxx-xxxx
- x must be a digit from 0-9
- Solution must be a one-liner bash command
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code