java.util.Scanner Import;
Import java.util.regex.Matcher;
Import java.util.regex.Pattern;
public class the RegExp {
public static void main (String [] args) {
// create input objects
Scanner in = new Scanner (the System.in);
System.out.println ( "Please input verification content:");
String = in.next STR ();
// definition rule
String regex = "[\\ w] {4,8}" ;
// call regExp method for receiving return value
Boolean B = regExp (STR, REGEX);
// the return value
{IF (B)
( "! verification is successful") System.out.println;
} {the else
the System.out .println ( "malformed!");
}
}
static boolean regExp public (String str, String regex) {
// call the static method compile (); Gets the regular expression compiler
Pattern = the p-Pattern.compile (regex);
// call matcher () method to get the regular expression verifier will be verified write a string
Matcher m = p.matcher (STR);
// check, call matches () method
boolean m.matches B = ();
// returns a boolean value
return b ;
}
}
JAVA regular expression template
Guess you like
Origin www.cnblogs.com/private-mingmie/p/11818928.html
Recommended
Ranking