Regex Pattern Matching

This program matches string with regex pattern.

Problem Statement

Write a Java program to check if string contains digits using regex.

Source Code

1 public class RegexPatternMatching {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Regex Pattern Matching");
4 String str="Eduinq123";
5 if(str.matches(".*\\d.*")) System.out.println("String contains digits");
6 else System.out.println("String does not contain digits");
7 }
8 }

Program Output

Eduinq Regex Pattern Matching
String contains digits

Explanation

The regex .*\d.* checks for the presence of at least one digit anywhere in the string, and the matches() method validates the string against this pattern, demonstrating regex pattern matching.

Quick Links to Explore