深入入门正则表达式(java) - 3 - 正则在java中的使用

来源:互联网 发布:漯河市淘宝运营招聘 编辑:程序博客网 时间:2024/06/07 08:55

jdk版本选为1.6

1.5,1.4中的正则bug较多

 

我们先来总结一下java正则流派的特性,这里直接完全引用《精通正则表达式》中的表格

1.字符缩略表示法

\a [\b] \e \f \n \r \t \0octal \x## \u#### \cchar  ---  \u####只运行4位16进制数字;\0octal要求开头是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

首先看看正则的编译

Java代码 复制代码 收藏代码
  1. Pattern regex = Pattern.compile(".*?", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);  

正则的编译相对来说很耗时 ,所以要注意复用。

第一个参数是正则,第二个是编译选项,可以同时指定多个,当然,也可以像下面这样什么也不指定

Java代码 复制代码 收藏代码
  1. Pattern regex = Pattern.compile(".*?");  

 

Matcher

我们把字符串传给matcher,然后设置各种条件,最后再用它干活

下面看看matcher都能干些什么

首先要获取Matcher对象

Java代码 复制代码 收藏代码
  1. Matcher matcher = pattern.matcher(str);  

中途要更换正则

Java代码 复制代码 收藏代码
  1. matcher.usePattern(newPattern);  

中途替换目标字符串

Java代码 复制代码 收藏代码
  1. matcher.reset(str);  

此时的matcher会丢失之前所有的明确的状态信息 - 比如下面要说到的搜索范围,之前匹配过的信息也就没有了

另一个相似函数,只是没有替换字符串而已

Java代码 复制代码 收藏代码
  1. matcher.reset();  

 

设定搜索范围

Java代码 复制代码 收藏代码
  1. matcher.region(start, end);   
  2. matcher.regionStart();   
  3. matcher.regionEnd();  

第一个用做设置搜索边界。默认为搜索整个字符串

后两个用来得到设置的边界位置

 

设置边界后的环视

Java代码 复制代码 收藏代码
  1. matcher.useTransparentBounds(bool);   
  2. matcher.hasTransparentBounds();  

如果设置了边界,那么环视查找时,是否允许检查环视外的字符可以通过上面的函数设置

默认为false,也就是说不考虑边界之外的字符。 

给一个简单的例子

目标字符串为abcde,我要查找b,但是要求b的前面是a。

如果边界设置为[1,5],也就是在bcde中查找,那么默认情况下是匹配不到结果的,,因为b已经在边界上了

但是如果允许在边界外检查,那么这里的b就符合要求

Java代码 复制代码 收藏代码
  1. String str = "abcde";   
  2. String regex = "(?<=a)b";   
  3. Pattern pattern = Pattern.compile(regex);   
  4. Matcher matcher = pattern.matcher(str);   
  5. matcher.region(15);//设置边界  
  6. System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看默认状态  
  7. System.out.println("find:" + matcher.find());//查找结果  
  8.   
  9. matcher.reset();//重置  
  10. System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态  
  11. matcher.useTransparentBounds(true);//设置  
  12. System.out.println("find:" + matcher.find());//查看结果  
  13.   
  14. matcher.reset();//重置  
  15. System.out.println("hasTransparentBounds:" + matcher.hasTransparentBounds());//查看重置后状态  

输出:

Xml代码 复制代码 收藏代码
  1. hasTransparentBounds:false   
  2. find:false   
  3. hasTransparentBounds:false   
  4. find:true   
  5. hasTransparentBounds:true  

我们可以看出,hasTransparentBounds默认是false

重置之后依然是false,当设置为true的时候再去重置,hasTransparentBounds没有改变

 

应用正则

查找

Java代码 复制代码 收藏代码
  1. matcher.find();   
  2. matcher.find(int);  

find():在当前检索范围应用正则。如果找到匹配,返回true,否则返回false。多次调用,则每次都从上次匹配之后的位置开始查找。

Java代码 复制代码 收藏代码
  1. String str = "are you a boy?";   
  2. String regex = "\\b\\w+\\b";   
  3. Pattern pattern = Pattern.compile(regex);   
  4. Matcher matcher = pattern.matcher(str);   
  5. while (matcher.find()) {   
  6.     System.out.println(matcher.group());   
  7. }  

结果:

Xml代码 复制代码 收藏代码
  1. are   
  2. you   
  3. a   
  4. boy  

 

find(int):参数为查找的起始偏移量。此函数不受当前检索范围影响,因为它调用了reset

Java代码 复制代码 收藏代码
  1. public boolean find(int start) {   
  2.     int limit = getTextLength();   
  3.     if ((start < 0) || (start > limit))   
  4.         throw new IndexOutOfBoundsException("Illegal start index");   
  5.     reset();   
  6.     return search(start);   
  7. }  

 

完全的匹配

Java代码 复制代码 收藏代码
  1. matcher.matches();  

正则如果能完全匹配目标字符串,那么返回true,否则返回false。匹配成功意味着匹配的结果为检索范围开始到检索范围结束的所有文本。

 

Java代码 复制代码 收藏代码
  1. matcher.lookingAt();  

与matches()类似,但是不要求检索范围内的整段文本都能匹配

 

匹配结果

Java代码 复制代码 收藏代码
  1. matcher.group();   
  2. matcher.group(int);   
  3. matcher.groupCount();  

group()返回上一次匹配的完整结果

group(int)返回上一次匹配中第N组的结果,如果N=0,那么同group()结果一样

Java代码 复制代码 收藏代码
  1. public String group() {   
  2.     return group(0);   
  3. }  

groupCount()返回捕获型括号的数目,组数

 

以下几个函数返回匹配结果的位置,其中无参的返回完整匹配的起始和结束位置,有参的返回分组匹配的起始和结束位置

Java代码 复制代码 收藏代码
  1. matcher.start();   
  2. matcher.start(int);   
  3. matcher.end();   
  4. matcher.end(int);  
 

替换

Java代码 复制代码 收藏代码
  1. matcher.replaceAll(String);   
  2. matcher.replaceFirst(String);  

返回目标字符串副本,其中匹配到的字符被替换

 

Java代码 复制代码 收藏代码
  1. matcher.appendReplacement(StringBuffer result, String replacement);   
  2. matcher.appendTail(StringBuffer result);  

appendReplacement:将上次匹配结束到这次匹配之前的字符串加入result,然后将这次匹配的内容替换为replacement后加入result

appendTail:找到所有匹配(或用户期望的匹配)后,将剩余的字符串加入result

 

下面是jdk6中的示例

Java代码 复制代码 收藏代码
  1. Pattern p = Pattern.compile("cat");   
  2. Matcher m = p.matcher("one cat two cats in the yard");   
  3. StringBuffer sb = new StringBuffer();   
  4. while (m.find()) {   
  5.     m.appendReplacement(sb, "dog");   
  6. }   
  7. m.appendTail(sb);   
  8. System.out.println(sb.toString());  

输出:

Xml代码 复制代码 收藏代码
  1. one dog two dogs in the yard  

红色为上次匹配之前到这次匹配之间的字符串

蓝绿色为这次匹配的字符串,将被替换成replacement

深蓝色为appendTail的工作

由于空格无法看出颜色,所以将空格用横线替代

 

过程为:

1."one- cat-two-cats-in-the-yard",result="one-dog"

2."one- cat-two- cat s-in-the-yard",result="one-dog-two-dogs"

3."one- cat-two- cats -in the yard",result="one-dog two-dogs-in-the-yard"

 

 

扫描程序

两个相关的api

Java代码 复制代码 收藏代码
  1. matcher.hitEnd();   
  2. matcher.requireEnd();   
  3.   
  4. /**  
  5.  * Boolean indicating whether or not more input could change 
  6.  * the results of the last match.  
  7.  *   
  8.  * If hitEnd is true, and a match was found, then more input 
  9.  * might cause a different match to be found. 
  10.  * If hitEnd is true and a match was not found, then more 
  11.  * input could cause a match to be found. 
  12.  * If hitEnd is false and a match was found, then more input 
  13.  * will not change the match. 
  14.  * If hitEnd is false and a match was not found, then more 
  15.  * input will not cause a match to be found. 
  16.  */  
  17. boolean hitEnd;   
  18.   
  19. /**  
  20.  * Boolean indicating whether or not more input could change 
  21.  * a positive match into a negative one. 
  22.  *  
  23.  * If requireEnd is true, and a match was found, then more 
  24.  * input could cause the match to be lost. 
  25.  * If requireEnd is false and a match was found, then more 
  26.  * input might change the match but the match won't be lost. 
  27.  * If a match was not found, then requireEnd has no meaning. 
  28.  */  
  29. boolean requireEnd;  

hitEnd:

如果为true,继续输入可能导致之前的匹配更改为一个新的匹配 (或者之前匹配成功,之后丢失匹配,匹配失败**) ,或者之前没有匹配后来有了匹配。

如果为false,继续输入则不会改变匹配结果。

关于**说明:变量上面的注释似乎没有说明这一点,但是《精通正则表达式》提及到了,**的结论是正确的。下面给出一个例子

Java代码 复制代码 收藏代码
  1. String subjectString = "1";   
  2. Pattern regex = Pattern.compile("^\\d$", Pattern.CASE_INSENSITIVE);   
  3. Matcher regexMatcher = regex.matcher(subjectString);   
  4. while(regexMatcher.find()){   
  5.     System.out.println(regexMatcher.group());   
  6.     System.out.println(regexMatcher.hitEnd());   
  7. }  

上面的例子中,我只想匹配一个数字,那么结果是能匹配到的,输出如下

Xml代码 复制代码 收藏代码
  1. 1   
  2. true  

 

如果目标字符串有两个数字,那么

Java代码 复制代码 收藏代码
  1. String subjectString = "12";   
  2. Pattern regex = Pattern.compile("^\\d$", Pattern.CASE_INSENSITIVE);   
  3. Matcher regexMatcher = regex.matcher(subjectString);   
  4. while(regexMatcher.find()){   
  5.     System.out.println(regexMatcher.group());   
  6.     System.out.println(regexMatcher.hitEnd());   
  7. }  

则没有输出

也就是说,hitEnd=true,并且之前是能找到匹配的,但是继续输入字符串,结果有可能变为无法找到匹配。

 

 

requireEnd:

如果为true,继续输入可能导致之前的丢失之前的匹配结果

如果为false,并且找到了匹配,更多的输入可能会导致之前的匹配内容改变,但是结果不会改变;如果没有找到匹配,那么此变量无意义。

 

 

最后看看Pattern的几个方法

Java代码 复制代码 收藏代码
  1. split(CharSequence input);   
  2. split(CharSequence input,int limit);  

split(CharSequence input):以input匹配到的内容做分割,返回分割好的数组

split(CharSequence input,int limit):分三种情况

1.limit<0:会保留结尾的空元素

2.limit=0:与split(CharSequence input)相同

3.limit>0:返回的数组最多为limit项,正则至多会应用limit-1次

下面对1和3举例说明:

Java代码 复制代码 收藏代码
  1. Pattern regex = Pattern.compile(",");   
  2. String[] ss = regex.split("a,b,c,d,",limit);   
  3. for (int i = 0; i < ss.length; i++) {   
  4.     System.out.println(ss[i]);   
  5. }  

limit=-1时,数组为5个元素:“a”,“b”,“c”,“d”和一个空字符串

limit=2时,数组为2个元素:“a”,“b,c,d,”,只应用了一次正则

 

编译参数

Java代码 复制代码 收藏代码
  1. regex.flags();  

返回compile时传递的参数

 

块转义:

\Q...\E 将\Q和\E之间的正则转义为字面意义。 比如正则:\Q[1]\E,表示的是匹配一对方括号,里面有一个数字1,而不是只有数字1的字符组。

下面的静态函数有同样的功效

Java代码 复制代码 收藏代码
  1. regex.quote(String s);  

例:

Java代码 复制代码 收藏代码
  1. System.out.println(Pattern.quote("[1]"));   
  2. //输出为\Q[1]\E  

 

查找:

Java代码 复制代码 收藏代码
  1. Pattern.matches(String regex, CharSequence input);  

 看了matches的源码我们就知道其含义了

Java代码 复制代码 收藏代码
  1. public static boolean matches(String regex, CharSequence input) {   
  2.     Pattern p = Pattern.compile(regex);   
  3.     Matcher m = p.matcher(input);   
  4.     return m.matches();   
  5. }  

 

 

至此java中的正则基本使用就介绍完了,希望大家拍砖的同时能给出意见,多谢

原创粉丝点击