Bash 0193.bash
egrep "^((\([[:digit:]]{3}\) )|([[:digit:]]{3}\-))[[:digit:]]{3}\-[[:digit:]]{4}$" file.txt
Difficulty
and has an acceptance rate of about 30% (400k+ submissions).
Typical patterns to solve this problem:
- Shell
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The command shown invokes egrep to filter lines from file.txt that conform to a specific telephone-number format. The regular expression is anchored at both the start (^) and end ($) of each line, ensuring that only complete matches are returned. By using the extended regular-expression syntax (egrep), the pattern can express alternation and grouping without excessive escaping, which keeps the expression concise and improves readability.
Key Steps
Invocation of the Pattern Matcher
egrep "^((\([[:digit:]]{3}\) )|([[:digit:]]{3}\-))[[:digit:]]{3}\-[[:digit:]]{4}$" file.txt
The command launches egrep, which reads file.txt line by line, applies the supplied pattern, and prints each line that satisfies the pattern. Because the pattern is quoted, the shell does not interpret any of the special characters, allowing the regular expression engine to process them directly.
Construction of the Regular Expression
The pattern consists of three logical parts: a leading area-code segment, a central-office segment, and a line-number segment.
The leading segment is expressed as an alternation between a parenthesized area code followed by a space and a plain three-digit area code followed by a hyphen:
((\([[:digit:]]{3}\) )|([[:digit:]]{3}\-))
The central-office segment is a fixed three-digit block:
[[:digit:]]{3}
The line-number segment is a fixed four-digit block preceded by a hyphen:
\-[[:digit:]]{4}
When concatenated and anchored, the full expression matches strings such as
(123) 456-7890
123-456-7890
File Processing
egrep streams the input file, evaluating each line independently. The anchored pattern guarantees that partial matches inside longer strings are ignored, so only lines that exactly match the telephone format are emitted.
Edge Cases Handled
The expression deliberately accounts for two common representations of North-American telephone numbers. By allowing either a parenthesized area code with a trailing space or a plain area code with a hyphen, the command accepts both styles without ambiguity. The use of [[:digit:]] rather than \d ensures compatibility with locales where digit character classes are defined, covering any Unicode digit that belongs to the current locale’s digit set. The anchors (^ and $) prevent matches on lines that contain additional characters before or after the phone number, such as surrounding whitespace or comments.
Performance Optimizations
Choosing egrep (the extended-regex variant) eliminates the need for backslashes before the alternation operator (|) and grouping parentheses, reducing the amount of parsing the regex engine must perform. The pattern is fully anchored, allowing the engine to reject non-matching lines early without scanning the entire line. The character class [[:digit:]] is a pre-compiled set, enabling fast membership tests for each character. By specifying exact quantifiers ({3} and {4}) the engine can allocate fixed-size buffers for the digit groups, avoiding the overhead of variable-length backtracking. The alternation is limited to two simple alternatives, each of which is a straightforward literal sequence, further minimizing the search space during matching.
ELI5 - Simplified Explanation
Imagine you have a giant toy box filled with hundreds of pieces of paper, and you want to find all the secret notes that have a phone number written on them. This little computer command is like a tiny robot detective that looks through your pile. It reads every single line in your file and checks to see if the characters on the page follow the special pattern of a phone number.
The part inside the quotes is a set of instructions for the detective. It tells the robot that it is looking for a specific shape, like a group of three numbers inside round parentheses or three numbers followed by a little dash. After that, it looks for three more numbers, then another dash, and finally four numbers at the end. If the detective sees a line that matches this exact pattern, it grabs that line and shows it to you on the screen so you can see it. It is just a very fast way to sort through a messy pile of information to find exactly what you need.
Code Quality
Complexity Analysis
Solved On The Same Day
- problem 175: Combine Two Tables
- problem 181: Employees Earning More Than Their Managers
- problem 182: Duplicate Emails
- problem 183: Customers Who Never Order
- problem 196: Delete Duplicate Emails
- problem 197: Rising Temperature
- problem 203: Remove Linked List Elements
- problem 511: Game Play Analysis I
- problem 577: Employee Bonus
- problem 584: Find Customer Referee
- problem 586: Customer Placing the Largest Number of Orders
- problem 595: Big Countries
- problem 596: Classes With at Least 5 Students
- problem 607: Sales Person
- problem 610: Triangle Judgement
- problem 619: Biggest Single Number
- problem 620: Not Boring Movies
- problem 627: Swap Sex of Employees
- problem 1050: Actors and Directors Who Cooperated At Least Three Times
- problem 1068: Product Sales Analysis I
- problem 1075: Project Employees I
- problem 1084: Sales Analysis III
- problem 1141: User Activity for the Past 30 Days I
- problem 1148: Article Views I
- problem 1179: Reformat Department Table
- problem 176: Second Highest Salary
- problem 177: Nth Highest Salary
- problem 178: Rank Scores
- problem 180: Consecutive Numbers
- problem 184: Department Highest Salary
- problem 192: Word Frequency
- problem 194: Transpose File
- problem 550: Game Play Analysis IV
- problem 570: Managers with at Least 5 Direct Reports
- problem 585: Investments in 2016
- problem 602: Friend Requests II: Who Has the Most Friends
- problem 608: Tree Node
- problem 626: Exchange Seats
- problem 725: Split Linked List in Parts
- problem 1045: Customers Who Bought All Products
- problem 1070: Product Sales Analysis III
- problem 1164: Product Price at a Given Date
- problem 1174: Immediate Food Delivery II
- problem 1193: Monthly Transactions I
- problem 1204: Last Person to Fit in the Bus
- problem 1321: Restaurant Growth
- problem 1393: Capital Gain/Loss
- problem 174: Dungeon Game
- problem 185: Department Top Three Salaries
- problem 218: The Skyline Problem
- problem 262: Trips and Users
- problem 601: Human Traffic of Stadium
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality