admin管理员组

文章数量:1579085

案例1:判断字符串是否是abc

packageRegex;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/*** 1.正则对应的类是Pattern(翻译:模式)而不是Rxgex,我们用到的方法都在Pattern类里面

* 2.案例1:判断字符串是否是abc*/

public classPatternDemo01 {public static voidmain(String[] args) {

String str= "abc";//根据以前的方法进行判断//System.out.println(str.equals("abc"));

/*** 用正则表达式来判断

* 1pile(String regex) 将给定的正则表达式编译到模式中。

* 2.matcher(CharSequence input) 创建匹配给定输入与此模式的匹配器。

* 3.matches() 尝试将整个区域与模式匹配。*/

//首先要编译正则规则形式

Pattern p = Patternpile("abc");//将正则进行匹配

Matcher m =p.matcher(str);//进行判断

boolean b =m.matches();

System.out.println(b);

}

}

案例2:判断一个字符串

packageRegex;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/*** 案例2:判断一个字符串

* 由3个字母组成

* 第一个字母是 a/b/c

* 第二个字母是 d/e/f/g

* 第三个字母是 x/y/z*/

public classPatternDemo02 {public static voidmain(String[] args) {

String str= "agx";//指定判断规则(中括号内字符顺序随便)

Pattern p = Patternpile("[abc][edgf][xzy]");//进行规则匹配

Matcher m =p.matcher(str);//进行判断

boolean b =m.matches();

System.out.println(b);

}

}

案例3:判断一个字符串

packageRegex;/*** 案例3:判断一个字符串

* 由3个字母组成

* 第一个字母是 a/b/c

* 第二个字母是 d/e/f/g

* 第三个字母是 x/y/z*/

public classPatternDemo03 {public static voidmain(String[] args) {

String str= "agx";//将原来的3个步骤封装到一步进行判断

System.out.println(str.matches("[abc][defg][zyx]"));

}

}

案例4:字符类

[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(交集)

[a-z&&[^bc]]     a 到 z,除了 b 和 c:[ad-z](减去)

[a-z&&[^m-p]]     a 到 z,而非 m 到 p:[a-lq-z](减去)

packageRegex;/*** [abc] a、b 或 c(简单类)

* [^abc] 任何字符,除了 a、b 或 c(否定)

* [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)*/

public classPatternDemo04 {public static voidmain(String[] args) {//判断由一个字母组成的字符串

String str1 = "D";

System.out.println(str1.matches("[a-zA-Z]"));//判断由一个数字组成的字符串

String num = "2";

System.out.println(num.matches("[0-9]"));//判断由一个字符组成的字符串,但不是 a/b/c

String str2 = "/";

System.out.println(str2.matches("[^a-c]"));

}

}

案例5:预定义字符类

packageRegex;/*** 案例5:预定义字符类

* . 任何一个字符(与行结束符可能匹配也可能不匹配)*/

public classPatternDemo05 {public static voidmain(String[] args) {//判断一个由a开头的由两个字符组成的字符串

String str1 = "af";

System.out.println(str1.matches("a."));//判断是否是一个 .//Java 用\\进行转义

String str2 = ".";

System.out.println(str2.matches("\\."));//匹配一个 \

String str3 = "\\";

System.out.println(str3.matches("\\\\"));

}

}

案例6:判断小数

packageRegex;/*** 案例6:Greedy 数量词

* X+ 代表 X至少出现一次或多次 >=1

*

* Reluctant 数量词

* X* 代表X出现零次或多次 >=0

*

* X? 表示X至多出现一次*/

public classPatternDemo06 {public static voidmain(String[] args) {//判断由a开头,至少有三个字符组成的字符串

String str1 = "asdfuj312s";

System.out.println(str1.matches("a.+"));//判断由小写字母开头,数字结尾的字符串

String str2 = "a3";

System.out.println(str2.matches("[a-z].*[0-9]"));//判断由a开头至多有2个字符组成的字符串

String str3 = "a";

System.out.println(str3.matches("a.?"));//判断小数

String str4 = "0.4";

System.out.println(str4.matches("[0-9]+\\.\\d+"));

System.out.println(str4.matches("0\\.\\d+") || str4.matches("[1-9]\\d*\\.\\d+"));

}

}

案例7:密码校验:8-20位,小写字母/大写字母/数字中的至少两种

packageRegex;/*** 案例7:

* X{n} X,恰好 n 次

* X{n,} X,至少 n 次

* X{n,m} X,至少 n 次,但是不超过 m 次*/

public classPatternDemo07 {public static voidmain(String[] args) {//判断由5个小写字母组成的字符串

String str1 = "gjhad";

System.out.println(str1.matches("[a-z]{5}"));//判断由至少5个小写字母组成的字符串

String str2 = "sdfguhvujhsdfkgvjhsdf";

System.out.println(str2.matches("[a-z]{5,}"));//判断由8-12个小写字母组成的字符串

String str3 = "weqrtyuio";

System.out.println(str3.matches("[a-z]{8,12}"));//练习:密码校验:8-20位,小写字母/大写字母/数字中的至少两种

String str4 = "qwe123SAD";

check(str4);

}public static booleancheck(String str) {//判断输入的密码是否出现规定以外的字符

if(!str.matches("[a-zA-Z0-9]{8,20}"))return false;//记录出现几种字符

int count = 0;//如果出现小写字母

if(str.matches(".*[a-z].*"))

count++;//如果出现大写字母

if(str.matches(".*[A-Z].*"))

count++;//如果出现数字

if(str.matches(".*[0-9].*")) //str.matches(".*\\d.*")

count++;//如果count < 2

if(count < 2) {

System.out.println("密码错误!");return false;

}else

return true;

}

}

案例8:()捕获组

packageRegex;/*** 案例8:

* 1.()捕获组

* 2.正则表达式会对其中的捕获组进行自动编号

* 3.从1开始编号

* 4.\\n 表示引用前边编号为n的捕获组

**/

public classPatternDemo08 {public static voidmain(String[] args) {/*** 捕获组的编号是从捕获组的左半边括号出现的位置开始计算的

* (A((BC)(D))E)(F)

* 1号:A((BC)(D))E

* 2号:(BC)(D)

* 3号:BC

* 4号:D

* 5号:F*/String str1= "abfdabasdf";

System.out.println(str1.matches(".*(ab).*\\1.*"));//匹配至少由两个字符组成的字符串

System.out.println(str1.matches("(.){2,}"));

}

}

案例9:运用捕获组判断叠字

packageRegex;/*** 案例9:

* 运用捕获组判断叠字*/

public classPatternDemo09 {public static voidmain(String[] args) {//匹配AAA形式的叠字字符串

String str1 = "aaa";

System.out.println(str1.matches("(.)\\1+"));//匹配AABB形式的叠字字符串

String str2 = "AABB";

System.out.println(str2.matches("(.)\\1(.)\\2"));//匹配ABAB形式的叠字字符串

String str3 = "ABAB";

System.out.println(str3.matches("(..)\\1"));

}

}

案例10:邮箱格式校验

packageRegex;/*** 案例10:邮箱格式校验

**/

public classPatternDemo10 {public static voidmain(String[] args) {

String str1= "42513@qq";

String str2= "cjjsdfasf@sina";

String str3= "24eqwe";

check(str1);

check(str2);

check(str3);

}public static voidcheck(String str) {//以结尾的邮箱

if(str.matches("[a-zA-Z0-9]{6,36}@[a-zA-Z0-9]{3,10}(\\)?(\\)")||str.matches("[a-zA-Z0-9]{3,10}@[a-zA-Z0-9]{1,36}(\\)")) {

System.out.println("ok");

}elseSystem.out.println("no");

}

}

案例11:利用正则表达式替换字符串内指定字符

packageRegex;/*** 利用正则表达式替换字符串内指定字符

**/

public classPatternDemo11 {public static voidmain(String[] args) {

String str= "261refdae612c,./d/";//将字符串内所有数字替换为-

System.out.println(str.replaceAll("\\d", "-"));//将字符串内所有非数字去掉

System.out.println(str.replaceAll("\\D", ""));

}

}

案例12:输入一个字符串,统计字符串中每一个字符出现的次数

packageRegex;/*** 输入一个字符串,统计字符串中每一个字符出现的次数

**/

public classPatternDemo12 {public static voidmain(String[] args) {

String str= "sdafv187623rtajhsd";

fin(str);

}public static voidfin(String str) {//获取第一个字符

char c = str.charAt(0);//将第一个字符都替换为空

String str1 = str.replaceAll("" + c, "");//计算两个字符串的长度

int len = str.length() -str1.length();

System.out.println(str.charAt(0) + ":" +len);//如果替换后的字符串长度大于0.说明还没有统计完,再次调用统计方法

if(str1.length() > 0)

fin(str1);

}

}

案例13:交换字符串中指定位置的字符

packageRegex;/*** 交换字符串中指定位置的字符

**/

public classPatternDemo13 {public static voidmain(String[] args) {

String str= "I Like Beijing.";

System.out.println(str.replaceAll("(I )(Like )(Beijing.)", "$3$2$1"));

}

}

案例14:将叠字替换为单字

packageRegex;/*** 将叠字替换为单字*/

public classPatternDemo14 {public static voidmain(String[] args) {

String str= "AAAAABBBBBDDDDDGGGGG";

System.out.println(str.replaceAll("(.)\\1+", "$1"));

}

}

案例15:计算字符串的平均长度

packageRegex;/*** 计算字符串的平均长度

**/

public classPatternDemo15 {public static voidmain(String[] args) {

String str= "aaabbbdjhjjjjkkkkk(((jjjooo";

fin(str);

}public static voidfin(String str) {//字符串长度

double len =str.length();//将叠字换成单字

String str1 = str.replaceAll("(.)\\1+", "a");//统计单字组成的长度

double len1 =str1.length();double n = len /len1;

System.out.println(n);

}

}

案例16:以数字作为切割符将字符切开

packageRegex;importjava.util.Arrays;/*** 以数字作为切割符将字符切开

* 作为切割符的符号会被切掉

* 在字符串最尾部的切割符会整个切掉*/

public classPatternDemo16 {public static voidmain(String[] args) {

String str= "2iasug8jhfch9sjba90";/*** 1.split(String regex)

* 根据给定正则表达式的匹配拆分此字符串。

* 2.toString(Object[] a)

* 返回指定数组内容的字符串表示形式。*/

//str1是字符串数组

String[] str1 = str.split("\\d");//将字符串数组抓换成字符串

System.out.println(Arrays.toString(str1));

}

}

本文标签: 正则表达式详解实例详细Java