正则表达式
正则表达式:其实一种规则,有自己特殊的应用,其作用就是针对于字符串进行操作。
正则:就是用于操作字符串的规则,其中这些规则使用了一些字符表示。
使用正则表达式:
public class Demo2{
public static void main(String[] args) {
//只能输入数字
String str = "12435423a2";
boolean flag = str.matches("[0-9]+");
System.out.println(flag?"输入正确":"只能输入数字");
}
}
正则表达式的符号
预定义字符类
. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
System.out.println("a".matches("."));
System.out.println("1".matches("\\d"));
System.out.println("%".matches("\\D"));
System.out.println("\r".matches("\\s"));
System.out.println("^".matches("\\S"));
System.out.println("a".matches("\\w"));
Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好n次
X{n,} X,至少n次
X{n,m} X,至少n次,但是不超过m次
System.out.println( "a".matches(".") );
System.out.println( "a".matches("a") );
System.out.println("a".matches("a?") );
System.out.println( "aaa".matches("a*") );
System.out.println( "".matches("a+") );
System.out.println( "aaaaa".matches("a{5}") );
System.out.println( "aaaaaaaaa".matches("a{5,8}") );
System.out.println( "aaa".matches("a{5,}") );
System.out.println( "aaaaab".matches("a{5,}") );
范围表示
[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](减去)
System.out.println( "a".matches("[a]") );
System.out.println( "aa".matches("[a]+") );
System.out.println( "abc".matches("[abc]{3,}") );
System.out.println( "abc".matches("[abc]+") );
System.out.println( "dshfshfu1".matches("[^abc]+") );
System.out.println( "abcdsaA".matches("[a-z]{5,}") );
System.out.println( "abcdsaA12".matches("[a-zA-Z]{5,}") );
System.out.println( "abcdsaA12".matches("[a-zA-Z0-9]{5,}") );
System.out.println( "abdxyz".matches("[a-c[x-z]]+"));
System.out.println( "bcbcbc".matches("[a-z&&[b-c]]{5,}"));
System.out.println( "tretrt".matches("[a-z&&[^b-c]]{5,}"));
需求:校验QQ号,要求:必须是5~15位数字,0不能开头。没有正则表达式之前
public static void checkQQ2()
{
String qq = "12345";
String reg = "[1-9][0-9]{4,14}";
boolean b = qq.matches(reg);
System.out.println("b="+b);
}
需求:匹配是否为一个合法的手机号码。
public static void checkTel()
{
String tel = "25800001111";
String reg = "1[35]\\d{9}";//在字符串中,定义正则出现\ 要一对出现。
boolean b= tel.matches(reg);
System.out.println(tel+":"+b);
}
切割功能
需求1:根据空格对一段字符串进行切割。
public static void splitDemo()
{
String str = "aa.bb.cc";
str = "-1 99 4 23";
String[] arr = str.split(" +");
for(String s : arr)
{
System.out.println(s);
}
}
需求2 :根据重叠词进行切割。
public static void splitDemo2()
{
String str = "sdqqfgkkkhjppppkl";
String[] arr = str.split("(.)\\1+");
for(String s : arr)
{
System.out.println(s);
}
}
注意:为了提高规则复用,用()进行封装,每一个括号都有一个编号,从1开始,为了复用这个规则。可以通过编号来完成该规则的调用。需要对编号数字进行转义。\1就代表获取1组规则。
替换功能
需求:把手机号替换成“*”号。
String str = "联系我:13567012119联系我:13567012119联系我:13567012119联系我:13567012119联系我:13567012119联系我:13567012119";
String reg= "1[34578]\\d{9}";
str = str.replaceAll(reg,"******");
System.out.println("替换后的帖子:"+ str);
练习二:我我….我…我.要…要要…要学….学学..学.编..编编.编.程.程.程..程
将字符串还原成 我要学编程。
public static void main(String[] args) {
String str="我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
//将.去除
str=str.replaceAll("\\.+", "");
System.out.println(str);
//多个重叠字保留一个
str=str.replaceAll("(.)\\1+", "$1");
System.out.println(str);
}
获取
获取需要使用到正则的两个对象:使用的是用正则对象Pattern 和匹配器Matcher。
用法:
范例:
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
步骤:
1,先将正则表达式编译成正则对象。使用的是Pattern类一个静态的方法。compile(regex);
2,让正则对象和要操作的字符串相关联,通过matcher方法完成,并返回匹配器对象。
3,通过匹配器对象的方法将正则模式作用到字符串上对字符串进行针对性的功能操作
需求:获取由3个字母组成的单词。
public static void main(String[] args) {
String str = "da jia zhu yi le,ming tian bu fang jia,xie xie!";
//想要获取由3个字母组成的单词。
//刚才的功能返回的都是一个结果,只有split返回的是数组,但是它是把规则作为分隔符,不会获取符合规则的内容。
//这时我们要用到一些正则对象。
String reg="\\b[a-z]{3}\\b";
Pattern pattern = Pattern.compile(reg);
Matcher m = pattern.matcher(str);
while(m.find())
{
System.out.println(m.start()+"...."+m.end());
System.out.println("sub:"+str.substring(m.start(),m.end()));
System.out.println(m.group());
}
}
练习3:校验邮件
public static void checkMail()
{
String mail = "abc123@sina.com.cn";
mail = "1@1.1";
String reg = "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
reg = "\\w+@\\w+(\\.\\w+)+";//简化的规则。笼统的匹配。
boolean b = mail.matches(reg);
System.out.println(mail+":"+b);
}