[abc]:
匹配abc中任意一个字符
[a-z]:
匹配小写字母a-z中的一个
[A-Z]:
匹配大写字母A-Z中的一个
[0-9]:
匹配数字0-9中的一个
组合匹配:
[a-zA-Z0-9]:
匹配a-z或者A-Z或者0-9之间的任意一个字符
[a-dm-p]:
匹配a-d或m-p之间的任意一个字符
排除:
[^abc]:
匹配除a、b、c之外的任意一个字符 //^在中括号里面表示非的意思
[^a-z]:
匹配除小写字母外的任意一个字符
public class Test {
public static void main(String[] args) {
//匹配abc中任意一个字符。
System.out.println("A".matches("[abc]")); //false
System.out.println("ab".matches("[abc]")); //false
System.out.println("a".matches("[abc]")); //true
System.out.println("---------------------------------------");
//[a-z]:匹配小写字母a-z中的一个
System.out.println("B".matches("[a-z]")); //false
System.out.println("b".matches("[a-z]")); //true
System.out.println("---------------------------------------");
//[a-zA-Z0-9]:匹配a-z或者A-Z或者0-9之间的任意一个字符
System.out.println("我".matches("[a-zA-Z0-9]")); //false
System.out.println("0".matches("[a-zA-Z0-9]")); //true
System.out.println("0".matches("[a-zA-Z0-9]")); //true
System.out.println("f".matches("[a-zA-Z0-9]")); //true
System.out.println("A".matches("[a-zA-Z0-9]")); //true
//这里加了一个空格,匹配范围就会更改为[a-z A-Z0-9]:
// 匹配a-z或者空格或者A-Z或者0-9之间的任意一个字符
System.out.println(" ".matches("[a-z A-Z0-9]")); //true
System.out.println("------------------------");
System.out.println("A".matches("[^A-Z]")); //false
System.out.println("a".matches("[^A-Z]")); //true
System.out.println("0".matches("[^A-Z]")); //true
}
}
".":
匹配一个任意字符
"d":
匹配一个数字字符,相当于[0-9]
"D":
匹配一个非数字,相当于[^0-9]
"s":
匹配一个空白字符
"S":
匹配一个非空白字符
"w":
匹配一个单词字符,包括大小写字母,数字,下划线,相当于 [a-zA-Z0-9_]
"W" :
匹配一个非单词字符
^ :
匹配字符串的开始(在集合字符里[^a]表示非(不匹配)的意思
$ :
匹配字符串的结束
注意:有的记得加,变成,表转义
如: d D s S w W
public class Test2 {
public static void main(String[] args) {
//匹配任意一个字符
System.out.println(".".matches(".")); //true
//匹配任意三个字符
System.out.println("a".matches("...")); //false
System.out.println("-----------------------------------------");
//匹配是数字开头和数字结束的三个数字字符
System.out.println("02a".matches("^ddd#34;)); //false
System.out.println("a12".matches("^ddd#34;)); //false
System.out.println("a1a".matches("^ddd#34;)); //false
System.out.println("250".matches("^ddd#34;)); //true
}
}
? :0次或1次
*: 0次或多次 (任意次)
+ :1次或多次
{n} :重复n次
{n,} :重复n次以上 (至少n次) >=n
{n,m} :重复n到m次(包括n和m)
//注意:限定符只能影响到离它最近的那个规则,如果想要影响整体,就给那个整体加()
// 如:
需要匹配 over!三次
System.out.println("over!over!over!".matches("over!{3}")); //false ,只能影响到!
System.out.println("over!over!over!".matches("(over!){3}")); //true ,影响到整体
public class Test3 {
public static void main(String[] args) {
//表示匹配a-z中的字符 "一次"
System.out.println("ab".matches("[a-z]?")); //false
System.out.println("a".matches("[a-z]?")); //true
System.out.println("----------------------------");
//表示匹配a-z中的字符 "任意次"
System.out.println("abcabc".matches("[a-z]*")); //true
System.out.println("-------------------------");
//表示匹配a-z中至少一次
System.out.println("ab".matches("[a-z]+")); //true
System.out.println("-------------------------------------");
//表示匹配任意4个数字字符
System.out.println("abcd".matches("d{4}")); //false
System.out.println("0123".matches("d{4}")); //true
System.out.println("----------------------------------");
//表示匹配任意一个字符5次以上
System.out.println("abc".matches("w{5,}")); //false
System.out.println("abcdefg".matches("w{5,}")); //true
System.out.println("-----------------------------------------");
//表示匹配任意一个单词字符5-7次
System.out.println("abc".matches("w{5,7}")); //false
System.out.println("abcdefgh".matches("w{5,7}")); //false
System.out.println("abcde".matches("w{5,7}")); //true
}
}
* 正则表达式是一个强大的字符串处理工具,是用于匹配字符串的模板,
* 可以对字符串进行格式校验,查找、提取、替换等操作
* boolean matches(String regex)
判断该字符串是否匹配指定的正则表达式
* String replaceAll(String regex, String replacement)
将该字符串中所有匹配正则表达式的内容替换成新的字符串,并返回替换后的新的字符串
* String[] split(String regex)
根据匹配规则,把字符串分割成多个子串
import java.util.Scanner;
public class Test4 {
/*
号码为18位,不能以数字0开头,前17位可以是数字,最后一位可以是数字或者大写字母X
*/
public static void main(String[] args) {
Scanner rd=new Scanner(System.in);
String a=rd.next();
Boolean b=a.matches("[1-9]d{16}[0-9X]");
System.out.println(b);
}
}
public class Main {
public static void main(String[] args) {
//邮箱案例
Scanner rd=new Scanner(System.in);
String a=rd.next();
boolean b=a.matches("^[a-zA-Z][a-zA-Z0-9_]{5,18}");
System.out.println(b);
}
}
页面更新:2024-04-16
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号