Chinaunix首页 | 论坛 | 博客
  • 博客访问: 742666
  • 博文数量: 130
  • 博客积分: 2951
  • 博客等级: 少校
  • 技术积分: 1875
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 18:32
文章分类

全部博文(130)

文章存档

2013年(1)

2012年(129)

分类: Java

2012-02-20 13:57:11

例子
  1. package intro.regex;

  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;

  4. public class RegexTest {

  5.     public static void main(String [] args){
  6.         Pattern pattern = Pattern.compile("a.*string");
  7.         Matcher matcher = pattern.matcher("a string");
  8.         
  9.         boolean didMatch = matcher.matches();
  10.         System.out.println(didMatch); //true
  11.         
  12.         int patternStartIndex = matcher.start(); //0
  13.         int patternEndIndex = matcher.end(); //8
  14.         System.out.println("Start:" + patternStartIndex + " end: " + patternEndIndex);
  15.         
  16.         matcher = pattern.matcher("a string with more than just the pattern");
  17.         didMatch = matcher.matches();
  18.         System.out.println(didMatch); //false
  19.         matcher = pattern.matcher("a string with more than just the pattern");
  20.         didMatch = matcher.lookingAt();
  21.         System.out.println(didMatch); //true
  22.         
  23.         pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+");
  24.         matcher = pattern.matcher("Here is a WikiWord followed by AnotherWikiWord, then YetAnotherWikiWord.");
  25.         while(matcher.find()){
  26.             System.out.println("Found whit wiki word: " + matcher.group());
  27.             //Found whit wiki word: WikiWord
  28.      //Found whit wiki word: AnotherWikiWord
  29.           //Found whit wiki word: YetAnotherWikiWord
  30.         }

  31.         System.out.println("*******************************************************");
  32.         matcher.reset();
  33.         StringBuffer buffer = new StringBuffer();
  34.         while(matcher.find()){
  35.             matcher.appendReplacement(buffer, "blah$0blah");
  36.             System.out.println(buffer.toString());
  37.      //Here is a blahWikiWordblah
  38.           //Here is a blahWikiWordblah followed by blahAnotherWikiWordblah
  39.            //Here is a blahWikiWordblah followed by blahAnotherWikiWordblah, then blahYetAnotherWikiWordblah
  40.         }
  41.         matcher.appendTail(buffer);
  42.         System.out.println(buffer.toString());
  43.         //Here is a blahWikiWordblah followed by blahAnotherWikiWordblah, then blahYetAnotherWikiWordblah.
  44.         System.out.println("*******************************************************");
  45.         
  46.     }
  47. }
几个要点:
1. Matcher.end()方法获得匹配到的子字符串的最后一个字符在字符串中的索引位置+1.
2. lookingAt()方法是从第一个字符看是不是有匹配的字符串,即使有匹配的,但不是从第一个字符开始,也不能匹配成功,如:
  1. Pattern pattern = Pattern.compile("a.*string");
  2. Matcher matcher = pattern.matcher("a string");
  3.         
  4. matcher = pattern.matcher("a string with more than just the pattern");
  5. boolean didMatch = matcher.matches();
  6. System.out.println(didMatch); //false
  7. matcher = pattern.matcher("a string with more than just the pattern");
  8. didMatch = matcher.lookingAt();
  9. System.out.println(didMatch); //true
  10. matcher = pattern.matcher("Here is a string with more than just the pattern");
  11. didMatch = matcher.lookingAt();
  12. System.out.println(didMatch); //false


阅读(1094) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~