Skip to content

Practice Valid Phone Numbers

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#193
LeetCode Problem

Valid Phone Numbers

Problem Statement

Write a one-line Bash command that reads file.txt and prints only lines matching either xxx-xxx-xxxx or (xxx) xxx-xxxx, where each x is a decimal digit. Input has one number per line and no leading or trailing whitespace. Preserve matching lines in input order. Practice approach: Use an extended regular expression anchored at both ends, with alternatives for the two complete formats. Escape literal parentheses and require the single space after a parenthesized area code. Partial matches must not accept extra digits or text.

Source: Valid Phone Numbers on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
222-333-4444
(444) 555-6666
222 333 4444
222-333-44444
Output
222-333-4444
(444) 555-6666
Explanation

Only the first two lines have an accepted complete format.

Constraints

One candidate phone number per line.
No leading or trailing whitespace.

Related LeetCode Problems

Practice more problems similar to Valid Phone Numbers.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.