**2018.10.20 阴
黑马程序员养成记录第一天
*
正则表达式:*
*定义:一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。
*作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个长度的事情就是正则表达式做的。
1.常见对象-(字符类)
字符类
*[abc] a,b,或c(简单类)
*[^abc] 任意字符,除了a,b或c(否定)
*[a-z A-Z] a到z或A到Z,两头的字母包括在内(范围)
*[0-9] 0到9的字符都包括
实现代码
package com.heima.Regex;
public class demo2T_Regex {
public static void main(String[] args) {
demo1();
System.out.println("----------");
demo2();
System.out.println("----------");
demo3();
System.out.println("----------");
demo4();
}
private static void demo4() {
String regex = "[0-9]";
System.out.println("1".matches(regex));
System.out.println("0".matches(regex));
System.out.println("d".matches(regex));
System.out.println("9".matches(regex));
}
private static void demo3() {
String regex = "[a-zA-Z]";
System.out.println("a".matches(regex));
System.out.println("z".matches(regex));
System.out.println("A".matches(regex));
System.out.println("1".matches(regex));
}
private static void demo2() {
String regex = "[^abc]";
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("d".matches(regex));
System.out.println("1".matches(regex));
}
private static void demo1() {
String regex = "[abc]";
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("d".matches(regex));
System.out.println("10".matches(regex));
}
}
}
private static void demo2() {
String regex = "[^abc]";
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("d".matches(regex));
System.out.println("1".matches(regex));
}
private static void demo1() {
String regex = "[abc]";
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("d".matches(regex));
System.out.println("10".matches(regex));
}
}
程序结果:
true
true
false
false
----------
false
false
true
true
----------
true
true
true
false
----------
true
true
false
true
2,常见对象(预定义字符)
A:预定义字符类
* . 任何字符。
* \d 数字:[0-9]
* \D 非数字 [^0-9]
* \s 空白字符[\t\n\r\x0B\f]
* \S 非空白字符 [^\s]
* \w 单词字符:[a-zA-Z_0-9]
* \W 非单词字符:[^\w]
实现代码:
package com.heima.Regex;
public class regex3_Regex {
public static void main(String[] args) {
demo1();
System.out.println("----------");
demo2();
System.out.println("----------");
demo3();
System.out.println("----------");
demo4();
System.out.println("----------");
demo5();
System.out.println("----------");
demo6();
System.out.println("----------");
demo7();
System.out.println("----------");
demo8();
}
private static void demo8() {
String regex = "\\W";
System.out.println("a".matches(regex)); //false
System.out.println("Z".matches(regex)); //false
System.out.println("_".matches(regex)); //false
System.out.println("0".matches(regex)); //false
System.out.println("%".matches(regex)); //true
}
private static void demo7() {
String regex = "\\w";
System.out.println("a".matches(regex)); //true
System.out.println("Z".matches(regex)); //true
System.out.println("_".matches(regex)); //true
System.out.println("0".matches(regex)); //true
System.out.println("%".matches(regex)); //false
}
private static void demo6() {
String regex = "\\S";
System.out.println(" ".matches(regex)); //false
System.out.println(" ".matches(regex));//table//false
System.out.println("a".matches(regex)); //true
}
private static void demo5() {
String regex = "\\s";
System.out.println(" ".matches(regex));//单空格 //true
System.out.println(" ".matches(regex));//tab键 //true
System.out.println(" ".matches(regex));//四个空格 //false
}
private static void demo4() {
String regex = "\\D";
System.out.println("9".matches(regex)); //false
System.out.println("0".matches(regex)); //false
System.out.println("a".matches(regex)); //true
}
private static void demo3() {
String regex = "\\d";
System.out.println("9".matches(regex)); //true
System.out.println("0".matches(regex)); //true
System.out.println("a".matches(regex)); //false
}
private static void demo2() {
demo3();
}
private static void demo1() {
String regex = "."; //一个点代表一个字符
System.out.println("a".matches(regex)); //true
System.out.println("ab".matches(regex)); //false
}
}
程序结果:
true
false
----------
true
true
false
----------
true
true
false
----------
false
false
true
----------
true
true
false
----------
false
false
true
----------
true
true
true
true
false
----------
false
false
false
false
true