正则表达式概述:
正则表达式也是一门语言,作用主要用于文本校验查找。
正则表达式使用:
String类型中有一个方法matches()在文档上可以查找对应的文档。
测试一:
@Test
public void test03(){
String s = "12";
//匹配0-9
// boolean matches = s.matches("[\\d]");
//匹配0-9
// boolean matches = s.matches("[0-9]");
//匹配非数字
boolean matches = s.matches("[\\D]");
System.out.println("匹配非数字 = " + matches);
//匹配空白
boolean matches1 = s.matches("[\\s]");
System.out.println("匹配空白 = " + matches1);
//匹配非空白 %^&*(
String s1 = "a";
boolean matches2 = s1.matches("[\\S]");
System.out.println("匹配非空白 = " + matches2);
//匹配单词:[a-zA-Z_0-9] 包括_
boolean matches3 = s1.matches("[\\w]");
System.out.println("匹配单词 = " + matches);
//匹配非单词
String s2 = "@";
// String s2 = "_";
boolean matches4 = s2.matches("[\\W]");
System.out.println("匹配非单词 " + matches4);
}
测试二:
+ : 出现一次或多次
*: 出现0次或多次
?: 出现0次或1次
\d{5}恰好出现5次
\d{3,}至少出现3次
\d{1,8} 可能出现一次到八次
[^ABC] 在[]里面^表示取反
^用在[]外边 或者正常正则表达式前 表示以什么开头
$ 表示以什么结尾
@Test
public void test05(){
// [^ABC]
String s = "A";
boolean matches = s.matches("[^ABC]");
System.out.println("matches = " + matches);
}
@Test
public void test04(){
/*
+ : 出现一次或多次
*: 出现0次或多次
?: 出现0次或1次
\d{5}恰好出现5次
\d{3,}至少出现3次
\d{1,8} 可能出现一次到八次
[^ABC] 在[]里面^表示取反
^用在[]外边 或者正常正则表达式前 表示以什么开头
$ 表示以什么结尾
*/
String s = "61";
// boolean matches = s.matches("[\\w]+");
boolean matches = s.matches("[\\w]");
//+ : 出现一次或多次
System.out.println("matches = " + matches);
String m = "1234";
// String m = "12345"; \d{5}恰好出现5次
boolean matches1 = m.matches("\\d{5}");
System.out.println("matches1 = " + matches1);
String phone = "12943071961";
//第一位是1第二位是【35789中的一位】后面9位是数字 但是写法不严谨
// boolean matches2 = phone.matches("1[35789]\\d{9}]");
boolean matches2 = phone.matches("^1[35789]\\d{9}$");
System.out.println("matches2 = " + matches2);
}
@Test
public void test03(){
String s = "12";
//匹配0-9
// boolean matches = s.matches("[\\d]");
//匹配0-9
// boolean matches = s.matches("[0-9]");
//匹配非数字
boolean matches = s.matches("[\\D]");
System.out.println("匹配非数字 = " + matches);
//匹配空白
boolean matches1 = s.matches("[\\s]");
System.out.println("匹配空白 = " + matches1);
//匹配非空白 %^&*(
String s1 = "a";
boolean matches2 = s1.matches("[\\S]");
System.out.println("匹配非空白 = " + matches2);
//匹配单词:[a-zA-Z_0-9] 包括_
boolean matches3 = s1.matches("[\\w]");
System.out.println("匹配单词 = " + matches);
//匹配非单词
String s2 = "@";
// String s2 = "_";
boolean matches4 = s2.matches("[\\W]");
System.out.println("匹配非单词 " + matches4);
}
@Test
public void test02(){
String s = "-";
boolean matches = s.matches("\\D");
System.out.println("matches = " + matches);
}
@Test
public void test01(){
String s = "1";
// boolean a = s.matches("A");
//[]可以匹配ABC
boolean a = s.matches("[ABC]");
//System.out.println("a = " + a);
//匹配 0-9
boolean matches = s.matches("[0-9]");
System.out.println("matches = " + matches);
//匹配[A-Z]匹配大写A到Z
//匹配[A-Za-z]匹配大写和小写字母
//匹配[A-Za-z0-9]匹配既有字母又有数字
//
}
测试三:
@Test
public void test6(){
String str = "1Hello233World334java4423atguigu5";
/* //Arrays.toString(split) = [, Hello, World, java, atguigu]
//以数字分割 会造成结果首位是,
// String[] split = str.split("[\\d]");
//System.out.println("Arrays.toString(split) = " + Arrays.toString(split));
*/
//将开头的数字和结尾的数字去掉
String s = str.replaceAll("^\\d|\\d$", "");
//一位数字切割
// String[] split = s.split("[0-9]");
// System.out.println("Arrays.toString(split) = " + Arrays.toString(split));
//+ 匹配一位或多位 * 匹配0位或多为 ? 匹配0位或1位
String[] split = s.split("\\d+");
System.out.println("Arrays.toString(split) = " + Arrays.toString(split));
}