How to Match Phone Numbers in a List to a Regex Pattern in Java ?

Regular Expressions are also known as regex or regexp. It is one of the powerful features of Java that helps in sequences of characters that define a search pattern. Nowadays it is widely used in computer science, programming, and text processing for tasks such as string matching, data extraction, and validation. In Java, the “java.util.regex” package set of tools for pattern matching.

In this article, we will learn how to match phone numbers in a list to a certain pattern using regex.

Java Program to Match Phone Numbers in the List to Regex Pattern

Step 1: Import All the Necessary Packages

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java. util.List;

Step 2: Define the Regex Pattern using Tokens in Java

Phone number contains only numeric value and some special characters according to countries. For example, Let’s take a U.S. phone number.

Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java. util.List;

class GFG {
    public static void main (String[] args) {
      
      String phoneNumberPattern = "\\b\\d{3}[-.]?\\d{3}[-.]?\\d{4}\\b";
        
    }
}