JAVA-正则表达式

jopen 8年前

Pattern.matches 方法适用于设置尚可接受并且只需进行一次测试的情况 (整个序列全匹配时才返回true)。

    boolean b = Pattern.matches("[T|t]rue", "true");System.out.println(b);true[Yy]es[Tt]rue时返回fale,由于不是整个序列匹配输入字符串"true"

  1. boolean a = "true".matches("[a-z]{4}");System.out.println(a);true

  2. Pattern pattern = Pattern.compile("[A-Z][a-z]*", Pattern.CASE_INSENSITIVE | Pattern.UNIX_LINES);

  3. split方法使用,分隔字符串

  4. Pattern pattern = Pattern.compile(",");String string = "sa,ad,fd,c,we,d,a";String[] strings = pattern.split(string);for (String s : strings) {    System.out.println(s);}

  5. Pattern对象是线程安全的,Matcher类对象是非线程安全的,因为它们在方法调用之间保有内状态。

  6. matcher对象可以重复使用,由于matcher非线程安全,所以需要在每次matcher时调用复位器rest()

  7. Pattern pattern = Pattern.compile("正则表达式");Matcher matcher = pattern.matcher("");String line;BufferedReader reader = new BufferedReader(new FileReader("文件路径"));while ((line = reader.readLine()) != null) {    //复位器,由于matcher非线程安全    matcher.reset(line);    if (matcher.find()) {        System.out.println("file == " + line);    }    reader.close();}

  8. Matcher中的matchers()与Pattern.matcher()用法相同,整个字符匹配才true;

  9. lookingAt(),匹配开始

    lookingAt( )方法与 matches( )相似,但是它不要求整个序列的模式匹配。如果正则表达式 模式匹配字符序列的beginning(开头),则lookingAt( )返回true。lookingAt( )方法往往从序列 的头部开始  。该方法的名字暗示了匹配程序正在 查看 目标是否以模式开头。如果返回 为true,那么可以调用start( )end( )group( )方法匹配的子序列的范围。

  10. find(),循环匹配

    find( )方法运行的是与 lookingAt( )相同类型的匹配操作,但是它会记 前一个匹配的位置 并在之后重新开始  。从而允许了相继调用 find( )对输入进行逐句比对,寻找 入的匹配。 复位后第一次调用该方法,则  将从输入序列的首个字符开始。在随后调用中,它将从前一 个匹配的子序列后面的第一个字符重新开始  。如各个调用来说,如果找到了模式将返回true;反之将返回 false。通常你会使用 find( )循环访问一些文本来查找其中所有匹配的模式。 

  11. //find循环匹配,返回开始结束位置,提取子序列    if (matcher.find( )) {      subseq = input.subSequence (matcher.start(), matcher.end( ));       }

  12. start(),end()需要在matches(),lookingAt(),find()调用后才有意义,匹配空字符串start=end

  13. group(),表达示捕获组,从左至右


  14. String match0 =input.subSequence (matcher.start(), matcher.end()).toString();  String match2 = input.subSequence (matcher.start (2),matcher.end (2)).toString()       //上述代码与下列代码等效:   //group()无参数,返回组0,即全部匹配项  String match0 = matcher.group();  String match2 = matcher.group(2);

  15. replaceFirst(),replaceAll()替换字符

    pattern.complie("[Bb]yte");

    String replace ="$lite";

    matcher.replaceFirst/replaceAll(replace);

  16. java中的转义字符需要增加两个反斜杠,\表示为\\,如果再使用正则来replace则需要再增加\\,最终结过两次转换过后\\\\代表一个 \。

  17. appendReplacement()  appendTail()

    appendReplacement() find后,复制查找的内容到新的StringBuffer,然后替换新的StringBuffer中的字符(不会理会原字符串中的未查找到的部份如何处理,只处理查找到的部份);

    appendTail() 刚好相反,复制查找的内容到新的StringBuffer,既处理查找部分字符,同样处理原字符串的余下部。份。

  18.     Pattern pattern = Pattern.compile("([Tt])hanks");      Matcher matcher = pattern.matcher("Thanks, thanks very much");      StringBuffer sb = new StringBuffer();      while (matcher.find()) {          if (matcher.group(1).equals("T")) {              matcher.appendReplacement(sb, "Thank you");          } else {              matcher.appendReplacement(sb, "thank you");          }      }      matcher.appendTail(sb);        System.out.println(sb.toString());  }
  19. String类已添加的公用正则表达式

    matches,split,relaceFirst,replaceAll

  20. Java正则是占有型量词 (possessive quantifier),它们比常规的贪婪量 (greedy quantifier)还要贪婪。占有型量词会尽可能地多匹配目标。

来自: http://my.oschina.net/u/2500345/blog/546405