Valid Phone Numbers

Solution For Valid Phone Numbers

Problem Statement:

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

Solution:

To solve this problem, we can use Regular Expressions (Regex) to match the pattern of valid phone numbers. We can use grep command with regex pattern to filter out only valid phone numbers from the given input.

Here is the one-liner bash script solution:

grep -P "^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$" file.txt

Explanation:

  • The ^ and $ characters are used to define the start and end of the line respectively.
  • The -P option with grep command is used to enable Perl-style regular expression pattern matching.
  • The \d symbol matches any digit from 0 to 9.
  • The {3} means that the preceding digit matches exactly 3 times.
  • The ( and ) characters are used to group digits together.
  • The | character is used to specify either grouping.
  • The space between the parentheses is optional.
  • The \ character is used to escape special characters to their literal equivalents.
  • The grep command matches and prints all phone numbers that meet the specified pattern.

Example:

Suppose the file.txt contains the following data:

987-123-4556
(123) 456-7890
mmmm 123) 456-7890
(111) 111-1111

The above bash script will produce the following output:

987-123-4556
(123) 456-7890

So, the output contains only the valid phone numbers from the given input.

Step by Step Implementation For Valid Phone Numbers

//Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

//You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

//You may also assume each line in the text file must not contain leading or trailing white spaces.

#!/bin/bash

grep -E '^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

For example, assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890
Your script should output the following valid phone numbers:
987-123-4567
(123) 456-7890
/**
 * @param {string} s
 * @return {string[]}
 */
var findNumbers = function(s) {
    let result = [];
    // your code goes here
    return result;
};
/**
 * Valid Phone Numbers
 * 
 * Given a text file file.txt that contains list of phone numbers (one per line), 
 * write a one liner bash script to print all valid phone numbers.
 * 
 * You may assume that a valid phone number must appear in one of the following two formats: 
 * (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
 * 
 * You may also assume each line in the text file must not contain leading or trailing white spaces.
 * 
 * For example, assume that file.txt has the following content:
 * 
 * 987-123-4567
 * 123 456 7890
 * (123) 456-7890
 * 
 * Your script should output the following valid phone numbers:
 * 987-123-4567
 * (123) 456-7890
 * 
 */

/**
 * Solution:
 * 
 * Use grep to filter the phone numbers that match the specified pattern:
 * 
 * grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}

public class Solution {
    public bool IsValid(string s) {
        // check for empty string
        if (string.IsNullOrEmpty(s)) {
            return false;
        }
        
        // define regex pattern for valid phone number
        string pattern = @"^\d{3}-\d{3}-\d{4}$";
        
        // use Regex.IsMatch to check if string s is a valid phone number
        return Regex.IsMatch(s, pattern);
    }
}

file.txt

file.txt
*
* Explanation:
*
* The pattern used in the grep command above is a Perl-compatible regular expression (PCRE),
* which matches the following phone number formats:
*
* xxx-xxx-xxxx
* (xxx) xxx-xxxx
*
* The first part of the pattern, ^(\d{3}-|\(\d{3}\) ), matches the following:
*
* A digit (\d) repeated 3 times, followed by a dash (-), OR
* An open parenthesis (\(), a digit repeated 3 times, and a close parenthesis (\)),
* followed by a space ( )
*
* The second part of the pattern, \d{3}-\d{4}$, matches a digit repeated 3 times,
* followed by a dash (-), and then a digit repeated 4 times.
*
* The ^ and $ anchors are used to specify that the pattern must match the
* beginning and end of the string, respectively.
*/

file.txt

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]