Java学习笔记之正则表达式(一):正则表达式的规则

来源:互联网 发布:管家婆单机版软件 编辑:程序博客网 时间:2024/05/16 18:33

package com.collection.regex;import org.junit.Test;/*正则表达式:正则表达式其实就是用于操作字符串的一个规则,使用特殊的符号表示;需求:校验一个QQ号:1、不能以0开头;2、长度为5~12位;3、只能由数字组成; */public class Demo1 {//String qq = "123";//String qq = "01234567";//String qq = "123abc4567";String qq = "1234567";// 该方法可以实现,但是太复杂,太麻烦@Testpublic void test1(){if (!qq.startsWith("0")){if (qq.length() >= 5 && qq.length() <= 12){try {Long.parseLong(qq);System.out.println("合法QQ");} catch (Exception e) {System.out.println("非法QQ,QQ只能由数字组成");}}else{System.out.println("非法QQ,QQ长度必须为5~12位");}}else{System.out.println("非法QQ,QQ不能以0开头");}}// 使用正则表达式进行校验@Testpublic void test2(){System.out.println(qq.matches("[1-9]\\d{4,11}")?"合法QQ":"非法QQ");}}


package com.collection.regex;import org.junit.Test;/* * 正则表达式规则: */public class Demo2 {/*预定义字符类:.任意字符\d数字:[0-9]\D非数字:[^0-9]\s空白字符:[\t\n\x0B\f\r]\S非空白字符:[^\s]\w单词字符:[a-zA-z_0-9]\W非单词字符:[^\w]注意:任何预定义字符在没有加上数量词之前,都只能匹配一个字符而已; */@Testpublic void test1(){System.out.println("------------ . -------------");// . 任意字符:只能匹配单个字符System.out.println("1".matches("."));// trueSystem.out.println("a".matches("."));// trueSystem.out.println("%".matches("."));// trueSystem.out.println("-".matches("."));// trueSystem.out.println("\\".matches("."));// true(两个\\表示一个\,前一个是转义字符)System.out.println("\r".matches("."));// false(只能匹配单个字符)System.out.println("haha".matches("."));// false(只能匹配单个字符)System.out.println("------------ \\d --------------");// \d 表示数字[0-9];要想\d表示自己所表达的意思,需要加一个转义字符(\),即写成 \\d;System.out.println("0".matches("\\d"));// trueSystem.out.println("8".matches("\\d"));// trueSystem.out.println("82".matches("\\d"));// false(只能匹配单个字符)System.out.println("a".matches("\\d"));// falseSystem.out.println("------------- \\D -------------");// \D 表示非数字^[0-9];只要不是数字,其他任何字符都可以;System.out.println("2".matches("\\D"));// falseSystem.out.println("a".matches("\\D"));// trueSystem.out.println("@".matches("\\D"));// trueSystem.out.println("+".matches("\\D"));// trueSystem.out.println("------------- \\s -------------");// \s 空白字符:[\t\r\n\x0B\f]System.out.println("\r".matches("\\s"));// trueSystem.out.println("\n".matches("\\s"));// trueSystem.out.println("\t".matches("\\s"));// trueSystem.out.println("\f".matches("\\s"));// trueSystem.out.println(" ".matches("\\s"));// trueSystem.out.println("a".matches("\\s"));// falseSystem.out.println("------------- \\S -------------");// \S 非空白字符:^[\s]System.out.println(" ".matches("\\S"));// falseSystem.out.println("a".matches("\\S"));// trueSystem.out.println("^".matches("\\S"));// trueSystem.out.println("------------- \\w -------------");// \w 单词字符:[a-zA-z_0-9]System.out.println("w".matches("\\w"));// true(小写字母可以)System.out.println("R".matches("\\w"));// true(大写字母可以)System.out.println("_".matches("\\w"));// true(下划线可以)System.out.println("7".matches("\\w"));// true(数字可以)System.out.println("@".matches("\\w"));// false(特殊字符不行)System.out.println("-".matches("\\w"));// false(运算符号不行)System.out.println("------------- \\W -------------");// \W 非单词字符:^[\w]System.out.println("w".matches("\\W"));// falseSystem.out.println("R".matches("\\W"));// falseSystem.out.println("_".matches("\\W"));// falseSystem.out.println("7".matches("\\W"));// falseSystem.out.println("@".matches("\\W"));// trueSystem.out.println("-".matches("\\W"));// true}/*数量词:写在{}里面(和范围词区分);?0次或1次*0次或多次+1次或多次{n}正好n次{n,}至少n次{n,m}至少n次,但不超过m次(n-m次) */@Testpublic void test2(){System.out.println("--------- ? ---------");// \\d? :出现0次或1次任意数字System.out.println("".matches("\\d?"));// trueSystem.out.println("2".matches("\\d?"));// trueSystem.out.println("28".matches("\\d?"));// falseSystem.out.println(" ".matches("\\d?"));// falseSystem.out.println("a".matches("\\d?"));// falseSystem.out.println("--------- * ---------");// \\d* :出现0次或多次任意数字System.out.println("".matches("\\d*"));// trueSystem.out.println("3".matches("\\d*"));// trueSystem.out.println("1234".matches("\\d*"));// trueSystem.out.println("&".matches("\\d*"));// falseSystem.out.println("--------- + ---------");// \\d+ :出现1次或多次任意数字System.out.println("".matches("\\d+"));// falseSystem.out.println("32".matches("\\d+"));// trueSystem.out.println("9876543210".matches("\\d+"));// trueSystem.out.println("--------- {n} ---------");// \\d{6} :只能出现6位任意数字System.out.println("123456".matches("\\d{6}"));// trueSystem.out.println("123".matches("\\d{6}"));// falseSystem.out.println("--------- {n,} ---------");// \\d{3,} :至少出现3位任意数字System.out.println("22".matches("\\d{3,}"));// falseSystem.out.println("223".matches("\\d{3,}"));// trueSystem.out.println("13241".matches("\\d{3,}"));// trueSystem.out.println("--------- {n,m} ---------");// \\d{3,6} :出现3-6位任意数字System.out.println("12".matches("\\d{3,6}"));// falseSystem.out.println("123".matches("\\d{3,6}"));// trueSystem.out.println("123456".matches("\\d{3,6}")); // trueSystem.out.println("1234567".matches("\\d{3,6}")); // false}/*范围词:写在中括号[]内;数量词写在大括号{}内;[abc] a、b或c[^abc]除了a、b或c之外的任何字符,否定词;[a-zA-Z] a-z或A到Z,两头的字母包括在内;[a-d[m-p]]a到d或m到p;可以直接写成[a-dm-p](并集);[a-z&&[def]]d、e或f;可以直接写成[def](交集);注意:范围词里面不管有多长,没有数量词的配合,都只能匹配一个字符; */@Testpublic void test3(){System.out.println("------- [abc] ------");// [abc] :出现abc中的其中一个字符;只能是一个字符;System.out.println("abc".matches("[abc]"));// falseSystem.out.println("ab".matches("[abc]"));// falseSystem.out.println("a".matches("[abc]"));// trueSystem.out.println("d".matches("[abc]"));// falseSystem.out.println("------- [^abc] ------");// [^abc] :除了a、b或c之外的任何字符,否定词;System.out.println("a".matches("[^abc]"));// falseSystem.out.println("&".matches("[^abc]"));// trueSystem.out.println("------- [a-zA-Z] ------");// [a-zA-Z] :26个字母中任意一个都可以,包括大小写System.out.println("w".matches("[a-zA-Z]"));// trueSystem.out.println("Z".matches("[a-zA-Z]"));// trueSystem.out.println("2".matches("[a-zA-Z]"));// falseSystem.out.println("&".matches("[a-zA-Z]"));// false}}

















原创粉丝点击