Java examples for Regular Expressions:Pattern
\w class allows underscores.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("[a-zA-Z0-9]"); Matcher matcher = pattern.matcher("2"); if (matcher.matches()) System.out.println("Match."); else/* w w w . j ava 2s .co m*/ System.out.println("Does not match."); } }