java-正则表达式方法

Java正则表达式和Perl的是最为相似的。 java.util.regex包主要包括以下三个类:

    Pattern类:
    pattern对象是一个正则表达式的编译表示。Pattern类没有公共构造方法。要创建一个Pattern对象,你必须首先调用其公共静态编译方法,它返回一个Pattern对象。该方法接受一个正则表达式作为它的第一个参数。

    Matcher类:
    Matcher对象是对输入字符串进行解释和匹配操作的引擎。与Pattern类一样,Matcher也没有公共构造方法。你需要调用Pattern对象的matcher方法来获得一个Matcher对象。

    PatternSyntaxException:
    PatternSyntaxException是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

捕获组
 捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。
 捕获组是通过从左至右计算其开括号来编号。例如,在表达式((A)(B(C))),有四个这样的组:

    ((A)(B(C)))
    (A)
    (B(C))
    (C)

可以通过调用matcher对象的groupCount方法来查看表达式有多少个分组。groupCount方法返回一个int值,表示matcher对象当前有多个捕获组。
还有一个特殊的组(组0),它总是代表整个表达式。该组不包括在groupCount的返回值中。

实例:

 1 import java.util.regex.Pattern;
 2 import java.util.regex.Matcher;
 3 
 4 public class RegexMatches{
 5     public static void main(String args[]){
 6         // 按指定模式在字符串查找
 7      String line = "This order was placed for QT3000! OK?";
 8 
 9      String pattern = "(.*)(\d+)(.*)";
10 
11      Pattern r = pattern.compile(pattern);// 创建 Pattern 对象
12      Matcher m = r.matcher(line); // 现在创建 matcher 对象
13 
14      if(m.find()){
15          System.out.println("Found value: " + m.group(0));
16          System.out.println("Found value: " + m.group(1));
17          System.out.println("Found value: " + m.group(2));
18      }
19      else{
20          System.out.println("not Found");
21      }
22     }
23 }
Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT300
Found value: 0


Mather类的方法
索引方法-start 和end 方法

 1 import java.util.regex.Pattern;
 2 import java.util.regex.Matcher;
 3 public class RegexMatches
 4 {
 5     private static final String REGEX = "\bcat\b";
 6     private static final String INPUT = "cat cat cat cattie cat";
 7 
 8     public static void main(String args[]){
 9         Pattern p = Pattern.compile(REGEX);
10         Matcher m = p.matcher(INPUT);
11         int count = 0;
12         while(m.find){
13             count++;
14             System.out.println("Match number"+count);
15             System.out.println("start():"+m.start());
16             System.out.println("end():"+m.end());
17         }
18     }


编译结果

Match number 1
start(): 0
end(): 3
Match number 2
start(): 4
end(): 7
Match number 3
start(): 8
end(): 11
Match number 4
start(): 19
end(): 22


索引方法-matches 和lookingAt 方法
matches 和lookingAt 方法都用来尝试匹配一个输入序列模式。它们的不同是matcher要求整个序列都匹配,而lookingAt 不要求。
这两个方法经常在输入字符串的开始使用。

 1 import java.util.regex.Pattern;
 2 import java.util.regex.Matcher;
 3 
 4 public class RegexMatches{
 5     private static final String REGEX = "foo";
 6     private static final String INPUT = "fooooooooooooo";
 7     private static Pattern pattern;
 8     private static Matcher matcher;
 9 
10     public static void main(String args[])
11     {
12         pattern = Pattern.compile(REGEX);
13         matcher = pattern.matcher(INPUT);
14 
15         System.out.println("current REGEX is:"+REGEX);
16         System.out.println("current INPUT is:"+INPUT);
17 
18         System.out.println("LockingAt():"+matcher.lockingAt());
19         System.out.println("matches():"+matcher.matches());
20     }
21 }

编译结果

current REGEX is: foo
current INPUT is: fooooooooooooooooo
lookingAt(): true
matches(): false

索引方法-replaceFirst 和replaceAll 方法
replaceFirst 和replaceAll 方法用来替换匹配正则表达式的文本。
不同的是,replaceFirst 替换首次匹配,replaceAll 替换所有匹配

 1 import java.util.regex.Pattern;
 2 import java.util.regex.Matcher;
 3 
 4 public class RegexMatches{
 5     private static String REGEX = "dog";
 6     private static String INPUT = "The dog says meow. "+"All dogs say meow.";
 7     private static String REPLACE = "cat";
 8     public static void main(String[] args) {
 9        Pattern p = Pattern.compile(REGEX);
10        //获取一个matcher object
11        Matcher m = p.matcher(INPUT);
12        INPUT = m.replaceAll(REPLACE);
13        System.out.println(INPUT);
14    }
}

编译结果
The cat says meow. All cats say meow.

索引方法-appendReplacement 和 appendTail 方法
Matcher 类也提供了appendReplacement 和appendTail 方法用于文本替换

 1 import java.util.regex.Matcher;
 2 import java.util.regex.Pattern;
 3 
 4 public class RegexMatches{
 5     private static String REGEX = "a*b";
 6     private static String INPUT = "aabfooaabfoob";
 7     private static String REPLACE ="-";
 8     public static void main(String args[]){
 9         Pattern p = Pattern.compile(REGEX);
10         Matcher m = p.matcher(INPUT);
11         StringBuffer sb = new StringBuffer();
12 
13         while (m.find()){
14             m.appendReplacement(sb,REPLACE);
15         }
16         m.appendTail(sb);
17         System.out.println(sb.toString();)
18     }

编译结果
-foo-foo-foo-



索引方法-PatternSyntaxException 类的方法
PatternSyntaxException 是一个非强制异常类,它指示一个正则表达式模式中的语法错误。
PatternSyntaxException 类提供了下面的方法来帮助我们查看发生了什么错误。
以下方法:
    public String getDescription()
获取错误的描述。
    public int getIndex()
 获取错误的索引。
     public String getPattern()
获取错误的正则表达式模式。
    public String getMessage()
返回多行字符串,包含语法错误及其索引的描述、错误的正则表达式模式和模式中错误索引的可视化指示。

原文地址:https://www.cnblogs.com/Terminaling/p/4069371.html