Java examples for Language Basics:String
Use the regular expression Pattern and Matcher classes.
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); } }