深入入门正则表达式(java) - 3 - 正则在java中的使用
深入入门正则表达式(java) - 引言 深入入门正则表达式(java) - 1 - 入门基础 深入入门正则表达式(java) - 3 - 正则在java中的使用 深入入门正则表达式(java) - 匹配原理 - 1 - 引擎分类与普适原则 深入入门正则表达式(java) - 匹配原理 - 2 - 回溯 jdk版本选为1.6 1.5,1.4中的正则bug较多 我们先来总结一下java正则流派的特性,这里直接完全引用《精通正则表达式》中的表格 1.字符缩略表示法 a [b] e f n r t octal x## u#### cchar ---u####只运行4位16进制数字; octal要求开头是0,后面接1至3为10进制数字;cchar是区分大小写的,直接对后面字符的十进制编码进行异或操作。 2.字符组及相关结构 字符组:[...],[^...],可包含运算符 几乎任何字符:点号(根据模式不同,含义不同) 字符组缩略表示法:w d s W D S ---w W只能识别ASCII字符 3.锚点及其他零长断言 行/字符串起始位置:^A 行/字符串结束位置:$z Z 当前匹配的起始位置:G 单词分解符:b B ---能够识别Unicode字符 环视结构:(?=...) (?!...) (?<=...) (?<!...) ---顺序环视结构中可以使用任意正则表达式,逆序环视中只能匹配长度有限的文本 4.注释及修饰模式 模式修饰符:(?mods-mods)允许出现的模式:x d s m i u 模式修饰范围:(?mods-mods:...) 注释:从#到行末(只有在启动时有效) ---只有在使用/x修饰符或者Pattern.COMMENTS选项时,#才算注释。没有转移的ASCII空白字符将被忽略。字符组内部的注释和空白字符也会被忽略 文字文本模式:Q...E 5.分组及捕获 捕获型括号:(...) 1 2... 仅分组的括号:(?:...) 固化分组:(?>...) 多选结构:| 匹配优先量词:* + ? {n} {m,n} {m,} 忽略优先量词:*? +? ?? {n}? {n,}? {m,n}? 占有优先量词:*+ ++ ?+ {n}+ {n,}+ {m,n}+ ps:其中标注为蓝绿色的内容将在之后的教程讲解 下面开始介绍java中的正则api 首先看看正则的编译 Pattern regex = Pattern.compile(".*?",Pattern.CASE_INSENSITIVE | Pattern.DOTALL); 正则的编译相对来说很耗时,所以要注意复用。 第一个参数是正则,第二个是编译选项,可以同时指定多个,当然,也可以像下面这样什么也不指定 Pattern regex = Pattern.compile(".*?"); Matcher 我们把字符串传给matcher,然后设置各种条件,最后再用它干活 下面看看matcher都能干些什么 首先要获取Matcher对象 Matcher matcher = pattern.matcher(str); 中途要更换正则 matcher.usePattern(newPattern); 中途替换目标字符串 matcher.reset(str); 此时的matcher会丢失之前所有的明确的状态信息 - 比如下面要说到的搜索范围,之前匹配过的信息也就没有了 另一个相似函数,只是没有替换字符串而已 matcher.reset(); 设定搜索范围 matcher.region(start,end); matcher.regionStart(); matcher.regionEnd(); 第一个用做设置搜索边界。默认为搜索整个字符串 后两个用来得到设置的边界位置 设置边界后的环视 matcher.useTransparentBounds(bool); matcher.hasTransparentBounds(); 如果设置了边界,那么环视查找时,是否允许检查环视外的字符可以通过上面的函数设置 默认为false,也就是说不考虑边界之外的字符。 给一个简单的例子 目标字符串为abcde,我要查找b,但是要求b的前面是a。 如果边界设置为[1,5],也就是在bcde中查找,那么默认情况下是匹配不到结果的,,因为b已经在边界上了 但是如果允许在边界外检查,那么这里的b就符合要求 String str = "abcde"; String regex = "(?<=a)b"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); matcher.region(1,5);//设置边界 System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看默认状态 System.out.println("find:" + matcher.find());//查找结果 matcher.reset();//重置 System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态 matcher.useTransparentBounds(true);//设置 System.out.println("find:" + matcher.find());//查看结果 matcher.reset();//重置 System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态 输出: hasTransparentBounds:false find:false hasTransparentBounds:false find:true hasTransparentBounds:true 我们可以看出,hasTransparentBounds默认是false 重置之后依然是false,当设置为true的时候再去重置,hasTransparentBounds没有改变 应用正则 查找 matcher.find(); matcher.find(int); find():在当前检索范围应用正则。如果找到匹配,返回true,否则返回false。多次调用,则每次都从上次匹配之后的位置开始查找。 String str = "are you a boy?"; String regex = "bw+b"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group()); } 结果: are you a boy find(int):参数为查找的起始偏移量。此函数不受当前检索范围影响,因为它调用了reset public boolean find(int start) { int limit = getTextLength(); if ((start < 0) || (start > limit)) throw new IndexOutOfBoundsException("Illegal start index"); reset(); return search(start); } 完全的匹配 matcher.matches(); 正则如果能完全匹配目标字符串,那么返回true,否则返回false。匹配成功意味着匹配的结果为检索范围开始到检索范围结束的所有文本。 matcher.lookingAt(); 与matches()类似,但是不要求检索范围内的整段文本都能匹配 匹配结果 matcher.group(); matcher.group(int); matcher.groupCount(); group()返回上一次匹配的完整结果 group(int)返回上一次匹配中第N组的结果,如果N=0,那么同group()结果一样 public String group() { return group(0); } groupCount()返回捕获型括号的数目,组数 以下几个函数返回匹配结果的位置,其中无参的返回完整匹配的起始和结束位置,有参的返回分组匹配的起始和结束位置 matcher.start(); matcher.start(int); matcher.end(); matcher.end(int); 替换 matcher.replaceAll(String); matcher.replaceFirst(String); 返回目标字符串副本,其中匹配到的字符被替换 matcher.appendReplacement(StringBuffer result,String replacement); matcher.appendTail(StringBuffer result); appendReplacement:将上次匹配结束到这次匹配之前的字符串加入result,然后将这次匹配的内容替换为replacement后加入result appendTail:找到所有匹配(或用户期望的匹配)后,将剩余的字符串加入result 下面是jdk6中的示例 Pattern p = Pattern.compile("cat"); Matcher m = p.matcher("one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb,"dog"); } m.appendTail(sb); System.out.println(sb.toString()); one dog two dogs in the yard 红色为上次匹配之前到这次匹配之间的字符串 蓝绿色为这次匹配的字符串,将被替换成replacement 深蓝色为appendTail的工作 由于空格无法看出颜色,所以将空格用横线替代 过程为: 1."one-cat-two-cats-in-the-yard",result="one-dog" 2."cat-two-cats-in-the-yard",result="one-dog-two-dogs" 3."-two-cats-in the yard",result="one-dogtwo-dogs-in-the-yard" 扫描程序 两个相关的api matcher.hitEnd(); matcher.requireEnd(); /** * Boolean indicating whether or not more input could change * the results of the last match. * * If hitEnd is true,and a match was found,then more input * might cause a different match to be found. * If hitEnd is true and a match was not found,then more * input could cause a match to be found. * If hitEnd is false and a match was found,then more input * will not change the match. * If hitEnd is false and a match was not found,then more * input will not cause a match to be found. */ boolean hitEnd; /** * Boolean indicating whether or not more input could change * a positive match into a negative one. * * If requireEnd is true,then more * input could cause the match to be lost. * If requireEnd is false and a match was found,then more * input might change the match but the match won't be lost. * If a match was not found,then requireEnd has no meaning. */ boolean requireEnd; hitEnd: 如果为true,继续输入可能导致之前的匹配更改为一个新的匹配(或者之前匹配成功,之后丢失匹配,匹配失败**),或者之前没有匹配后来有了匹配。 如果为false,继续输入则不会改变匹配结果。 关于**说明:变量上面的注释似乎没有说明这一点,但是《精通正则表达式》提及到了,**的结论是正确的。下面给出一个例子 String subjectString = "1"; Pattern regex = Pattern.compile("^d$",Pattern.CASE_INSENSITIVE); Matcher regexMatcher = regex.matcher(subjectString); while(regexMatcher.find()){ System.out.println(regexMatcher.group()); System.out.println(regexMatcher.hitEnd()); } 上面的例子中,我只想匹配一个数字,那么结果是能匹配到的,输出如下 1 true 如果目标字符串有两个数字,那么 String subjectString = "12"; Pattern regex = Pattern.compile("^d$",sans-serif; font-size: 14px; line-height: 21px; ">则没有输出 |