1. 程式人生 > >Java之正則表達式

Java之正則表達式

star lar 靜態 rep color out under 模式 一個

  1 package test_demo.zhengzebiaodashi;
  2 
  3 import org.junit.Test;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 
  7 public class ZhengZeClass {
  8 
  9 /*
 10  *  java.util.regex 包主要包括以下三個類:
 11  *  Pattern 類:
 12  *      pattern 對象是一個正則表達式的編譯表示。Pattern 類沒有公共構造方法。
13 * 要創建一個 Pattern 對象,你必須首先調用其公共靜態編譯方法,它返回一個 Pattern 對象。該方法接受一個正則表達式作為它的第一個參數。 14 * Matcher 類: 15 * Matcher 對象是對輸入字符串進行解釋和匹配操作的引擎。與Pattern 類一樣,Matcher 也沒有公共構造方法。 16 * 你需要調用 Pattern 對象的 matcher 方法來獲得一個 Matcher 對象。 17 * PatternSyntaxException: 18 * PatternSyntaxException 是一個非強制異常類,它表示一個正則表達式模式中的語法錯誤。
19 * 詳細查看:http://www.runoob.com/java/java-regular-expressions.html 20 */ 21 22 /** 23 * 判斷文本是否包含某字符串 24 * -使用了正則表達式 .*runoob.* 用於查找字符串中是否包了 runoob 子串 25 */ 26 @Test 27 public void testMatches() { 28 String content = "I am noob " + 29 "from runoob.com.";
30 System.out.println("content:" + content); 31 32 String pattern = ".*runoob.*"; 33 System.out.println("pattern:" + pattern); 34 35 boolean isMatch = Pattern.matches(pattern, content); 36 System.out.println("字符串中是否包含了 ‘runoob‘ 子字符串? " + isMatch); 37 } 38 39 /** 40 * 分割字符串 41 * -Matcher.find()方法是部分匹配;Matcher.matches()方法是全部匹配 42 * -group(0)表示整個表達式 43 */ 44 @Test 45 public void testMatchesGroup() { 46 // 按指定模式在字符串查找 47 String content = "This order was placed for QT3000! OK?"; 48 String pattern = "(\\D*)(\\d+)(.*)"; 49 // 創建 Pattern 對象 50 Pattern r = Pattern.compile(pattern); 51 // 現在創建 matcher 對象 52 Matcher m = r.matcher(content); 53 if (m.find()) { 54 System.out.println("group(0) value: " + m.group(0)); 55 System.out.println("group(1) value: " + m.group(1)); 56 System.out.println("group(2) value: " + m.group(2)); 57 System.out.println("group(3) value: " + m.group(3)); 58 System.out.println("分組總數:: " + m.groupCount()); 59 } else { 60 System.out.println("NO MATCH"); 61 } 62 } 63 64 @Test 65 public void testMatchesHbasePut() { 66 String content = "put ‘t_test‘,‘test-ugid‘,‘f1:phone_no‘,‘15261851357‘"; 67 String putPattern = "^put\\s*‘(.*?)‘\\s*,\\s*‘(.*?)‘\\s*,\\s*‘f1:(.*?)‘\\s*,\\s*‘(.*?)‘$"; 68 Pattern p = Pattern.compile(putPattern); 69 Matcher m = p.matcher(content); 70 // matches()方法是全部匹配 71 if (m.matches()) { 72 System.out.println("group(0) value: " + m.group(0)); 73 System.out.println("分組總數:: " + m.groupCount()); 74 for (int i = 1; i <= m.groupCount(); i++) { 75 System.out.println("group(" + i + ") value: " + m.group(i)); 76 } 77 } 78 } 79 80 /** 81 * 統計匹配次數 82 * -Start方法返回之前的匹配操作期間,由給定組所捕獲的子序列的初始索引; 83 * -end方法為最後一個匹配字符的索引加1。 84 */ 85 @Test 86 public void testMatchNumber() { 87 // 匹配一個字邊界,即字與空格間的位置。 88 String REGEX = "\\bcat\\b"; 89 String INPUT = "cat cat cat cattie cat"; 90 91 Pattern p = Pattern.compile(REGEX); 92 Matcher m = p.matcher(INPUT); // 獲取 matcher 對象 93 int count = 0; 94 95 while (m.find()) { 96 count++; 97 System.out.println("Match number: " + count); 98 // Start方法返回之前的匹配操作期間,由給定組所捕獲的子序列的初始索引。 99 System.out.println("start(): " + m.start()); 100 // end方法為最後一個匹配字符的索引加1。 101 System.out.println("end(): " + m.end()); 102 } 103 } 104 105 /** 106 * 文本替換 107 * -replaceFirst方法用來替換首次匹配正則表達式的文本; 108 * -replaceAll方法用來替換所有匹配正則表達式的文本。 109 */ 110 @Test 111 public void testReplaceAll() { 112 String REGEX = "dog"; 113 String INPUT = "The dog says meow. All dogs say meow."; 114 String REPLACE = "cat"; 115 116 Pattern p = Pattern.compile(REGEX); 117 Matcher m = p.matcher(INPUT); 118 // INPUT = m.replaceFirst(REPLACE); 119 String output = m.replaceAll(REPLACE); 120 121 System.out.println("替換前:" + INPUT); 122 System.out.println("替換後:" + output); 123 } 124 125 }

Java之正則表達式