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 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 dashes

Where 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
Regex Pattern Matching ProcessInput File (file.txt)987-123-4567(555) 123-4567123 456 7890(123)456-7890555-1234-567Regex Pattern^(\(\d{3}\) |\d{3}-)\d{3}-\d{4}$Area CodeExchangeNumberValid Matches987-123-4567 โœ“(555) 123-4567 โœ“Only valid formats passOne-Liner Bash Commandgrep -E '^(\(\d{3}\) |\d{3}-)\d{3}-\d{4}$' file.txtInstantly filters and prints only valid phone numbers
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)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using variables for current line processing

n
2n
โœ“ 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
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
28.5K Views
Medium Frequency
~8 min Avg. Time
832 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen