1.
3.切割
[Ljava.lang.String;@4e57dc21
[明, 天, 放, 假]
[大, , 天, , 玩]
/*
正则表达式用来操作字符串的规则
预定义字符:
. 任意字符
\d 数字0-9
\D 非数字
\s 空白字符
\S 非空白字符
\w 单词字符 azAZ_09
\W 非单词字符
数量词:
? 一次或一次也没有
* 零次或多次
+ 一次或多次
{m} 恰好m次
{m,} 至少m次
{m,n} 至少m次 至多n次
范围词:
[abc]
[^abc]
[a-zA-Z] 并集
*/
public class Zhengze1 {
public static void main(String[] args) {
System.out.println("a".matches("."));
System.out.println("7".matches("\\d"));//没有加上数量词之前,都只能匹配一个字符而已
System.out.println("$".matches("\\D"));
System.out.println("\r".matches("\\s"));
System.out.println("2".matches("\\S"));
System.out.println("_".matches("\\w"));
System.out.println("%".matches("\\W"));
System.out.println("1".matches("\\d?"));
System.out.println("".matches("\\d*"));
System.out.println("123".matches("\\d+"));
System.out.println("1234567".matches("\\d{6,7}"));
System.out.println("abc".matches("[abc]"));//false 没有数量词的配合 都只能匹配一个
System.out.println("d".matches("[abc]"));//false
System.out.println("abc".matches("[abc]{3}"));
System.out.println("a".matches("[^abc]"));//false
System.out.println("Z".matches("[a-zA-Z]"));
System.out.println("@#%&".matches("[a-zA-Z@#&%$]{4}"));
}
}
2.判断手机号
public class ZHENGZE2 {
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>matchPhone("18856017129");
<span style="white-space:pre"> </span>matchPhone1("0372-6215821");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void matchPhone(String phone)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//正则匹配一个手机号 第一位只能是1开头第二位3 4 5 7 8 剩下的只要是数字 长度11位
<span style="white-space:pre"> </span>String reg = "1[34578]\\d{9}";
<span style="white-space:pre"> </span>System.out.println(phone.matches(reg)?"1":"0");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void matchPhone1(String phone)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//固定电话 区号-主机号 区号:首位是0 长度3-4 主机号:首位不能是0 长度7-8位
<span style="white-space:pre"> </span>String reg = "0\\d{2,3}-[^0]\\d{6,7}";
<span style="white-space:pre"> </span>//[1-9] = [^0]
<span style="white-space:pre"> </span>System.out.println(phone.matches(reg)?"1":"0");
<span style="white-space:pre"> </span>}
}
3.切割
import java.util.Arrays;
public class Qiege {
public static void main(String[] args) {
tesePlit1();
testPlit2();
}
public static void tesePlit1()
{
String str = "明 天 放 假";
//str.split(" "); 只按照一个空格切割不行
String[] datas = str.split(" +");
System.out.println(datas);
System.out.println(Arrays.toString(datas));
}
public static void testPlit2()
{
//根据重叠次进行切割
String str = "大家家明明明天好好好好好好哈哈哈哈哈哈玩";
//\1是第一组
//如果正则的内容需要被复用 需要对正则的内容进行分组 分组的目的就是为了提高正则的复用性
String[] datas = str.split("(.)\\1+");//引用第1组所匹配到的内容
//(.)是先读取一个 \\1+如 : 家+
System.out.println(Arrays.toString(datas));
}
}
[Ljava.lang.String;@4e57dc21
[明, 天, 放, 假]
[大, , 天, , 玩]