锁定老帖子 主题:正则表达式初步
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-04
1 为什么用正则表达式? String已经有了这个 String s.matchers(reg);和String s.replaceall(reg,“”) 使用pattern和matcher两个类的原因在于上面的字符串处理能力有限,丰富了其它的很多的方法,提高处理的能力。其次是提高程序代码执行的效率,先把pattern编译。 2 正则表达式基础 基本的正则表达式的:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {
public static void main(String[] args) { //简单认识正则表达式的概念 /* p("abc".matches("...")); p("a8729a".replaceAll("\\d", "-")); Pattern p = Pattern.compile("[a-z]{3}"); Matcher m = p.matcher("fgh"); p(m.matches()); p("fgha".matches("[a-z]{3}")); */
//初步认识. * + ? /* p("a".matches(".")); p("aa".matches("aa")); p("aaaa".matches("a*")); p("aaaa".matches("a+")); p("".matches("a*")); p("aaaa".matches("a?")); p("".matches("a?")); p("a".matches("a?")); p("214523145234532".matches("\\d{3,100}")); p("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")); p("192".matches("[0-2][0-9][0-9]")); */
//范围 /* p("a".matches("[abc]")); p("a".matches("[^abc]")); p("A".matches("[a-zA-Z]")); p("A".matches("[a-z]|[A-Z]")); p("A".matches("[a-z[A-Z]]")); p("R".matches("[A-Z&&[RFG]]")); */
//认识\s \w \d \ /* p(" \n\r\t".matches("\\s{4}")); p(" ".matches("\\S")); p("a_8".matches("\\w{3}")); p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+")); p("\\".matches("\\\\")); */
//POSIX Style //p("a".matches("\\p{Lower}"));
//boundary //^在【】中表示非的意思,在外面表示开头的意思 /* p("hello sir".matches("^h.*")); p("hello sir".matches(".*ir$")); p("hello sir".matches("^h[a-z]{1,3}o\\b.*")); p("hellosir".matches("^h[a-z]{1,3}o\\b.*")); //whilte lines p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));
p("aaa 8888c".matches(".*\\d{4}.")); p("aaa 8888c".matches(".*\\b\\d{4}.")); p("aaa8888c".matches(".*\\d{4}.")); p("aaa8888c".matches(".*\\b\\d{4}.")); */
//p("asdfasdfsafsf@dsdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
//matches find lookingAt //matches是找整个字符串,find是找字串,lookingAt每次从头开始找 //matches和find一起用可能会产生冲突,matches之后find接着从matches不匹配//的位置开始找,因此要用reset一下 /* Pattern p = Pattern.compile("\\d{3,5}"); String s = "123-34345-234-00"; Matcher m = p.matcher(s); p(m.matches()); m.reset(); p(m.find()); p(m.start() + "-" + m.end()); p(m.find()); p(m.start() + "-" + m.end()); p(m.find()); p(m.start() + "-" + m.end());//用的时候确保能够找的出来,不然用这个方法会报错 p(m.find()); //p(m.start() + "-" + m.end()); p(m.lookingAt()); p(m.lookingAt()); p(m.lookingAt()); */
//replacement /* Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf"); StringBuffer buf = new StringBuffer(); int i=0; while(m.find()) { i++; if(i%2 == 0) { m.appendReplacement(buf, "java"); } else { m.appendReplacement(buf, "JAVA"); } } m.appendTail(buf); p(buf); */
//group /* Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})"); String s = "123aa-34345bb-234cc-00"; Matcher m = p.matcher(s); while(m.find()) { p(m.group()); } */
//qulifiers /* Pattern p = Pattern.compile(".{3,10}+[0-9]"); String s = "aaaa5bbbb68"; Matcher m = p.matcher(s); if(m.find()) p(m.start() + "-" + m.end()); else p("not match!"); */
//non-capturing groups /* Pattern p = Pattern.compile(".{3}(?=a)"); String s = "444a66b"; Matcher m = p.matcher(s); while(m.find()) { p(m.group()); } */
//back refenrences /* Pattern p = Pattern.compile("(\\d(\\d))\\2"); String s = "122"; Matcher m = p.matcher(s); p(m.matches()); */
//flags的简写 //Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); p("Java".matches("(?i)(java)")); }
public static void p(Object o) { System.out.println(o); }
} 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 2801 次