Use the regular expression Pattern and Matcher classes. - Java Language Basics

Java examples for Language Basics:String

Description

Use the regular expression Pattern and Matcher classes.

Demo Code

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

public class Main {
  public static void main(String[] args) throws Exception {
    String str = "I love Java 9!"; 
    boolean result = false; 

    Pattern pattern = Pattern.compile("I love .*[ 0-9]!"); 
    Matcher matcher = pattern.matcher(str); 
    result = matcher.matches(); /*from ww w . java 2  s  .co  m*/

    System.out.println(result); 

    
  }
}

Result


Related Tutorials