黑马程序员-
------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------
正则表达式
正则表达式是用于专门操作字符串。用一些特定的符号来表示一些代码操作,这样就简化书写和对字符串的复杂操作但是其符号定义越多,正则越长,阅读性越差
使用方法:
1.匹配 String matches方法 用规则匹配整个字符串,只要有一个不符合规则 返回false
2.切割 spilt();
3.替换 replacAllDemo();
4.获取,将字符串中的符合规则的子串取出
其中"(.)\\1+","$1");组概念 把.封装成组,后面引用组,就是后面的字符和前面的字符只要有一样的就拿前面的字符替换成一个
public final class Patternextends Objectimplements Serializable正则表达式的编译表示形式。
String str = "heimachengxuyuan,haibucuo";
String reg ="\\b[a-z]{4}\\b";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while (m.find())
{
System.out.println(m.group());
}
这个获取流程就是,形象化来说,先有要匹配的对象,和规则,使用正则的compile方法和规则相关联,获取模式,调用里面的mather方法进行匹配操作。
------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------