/** * * A:正则表达式 * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。 * 作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的 * B:案例演示 * 需求:校验qq号码. * 1:要求必须是5-15位数字 * 2:0不能开头 * 3:必须都是数字 * a:非正则表达式实现 * b:正则表达式实现 */
public class Demo1_regex {public static void main (string[] args) {System.out.println (CHECKQQ ("012345")); System.out.println (CHECKQQ ("a1b345")); System.out.println (CHECKQQ ("123456")); System.out.println (CHECKQQ ("1234567890987654321")); String regex = "[1-9]\\d{4,14}"; System.out.println ("2553868". Matches (regex)); System.out.println ("012345". Matches (regex)); System.out.println ("2553868abc". Matches (regex)); }/* Required: Check the QQ number. * 1: Required must be 5-15 digits * 2:0 cannot start * 3: Must all be numbers * Check QQ * 1, explicit return value type Boolean * 2, explicit parameter list string QQ */Public s Tatic Boolean checkqq (String QQ) {Boolean flag = true; If the check QQ does not meet the requirements of the flag is set to False if required to return directly if (Qq.length () >= 5 && qq.length () <=) {if (!qq.s Tartswith ("0")) {char[] arr = Qq.tochararray (); Converts a string into a character array for (int i = 0; i < arr.length; i++) {char ch = arr[i]; Record each character if (!) ( Ch >= ' 0 ' && ch <= ' 9 ')) {flag = false; Not a number break; }}}else {flag = false; Starting with 0, not in line with the QQ standard}}ELSE {flag = false; Length does not match} return flag; }}
Java Regular Expression learning