標籤:指定 返回 構造 添加 index 語言 替換字串 匹配 except
以下所寫的內容在菜鳥教程中可以找到。如果有需要可以去看看。
Regex定義了字串模式,可以用來搜尋,編輯或處理文本;它不僅限於某一種語言,但是在每一種語言中有細微的差別。
java.util.regex包主要包括以下三個類:
1.Pattern類:pattern對象是一個Regex的編譯表示。Pattern類沒有公用構造方法。要建立一個pattern對象,你必須首先調用公用靜態編譯方法,
它返回一個Pattern對象。改方法接受一個Regex作為它的第一個參數。
2.Matcher: Matcher對象是對輸入字串進行解釋和匹配操作的引擎。與Pattern類一樣,Matcher也沒有公用構造方法。你需要調用Pattern對象的matcher
方法來獲得一個Matcher對象。
3.PatternSyntaxException:PatternSyntaxException是一個非強制的異常類,它表示一個正則模式中的語法錯誤。
擷取的群組
擷取的群組是把多個字元當一個單獨單元進行處理的方法,它通過對括弧內的字元分組來建立。
例如,Regex (dog) 建立了單一分組,組裡包含"d","o",和"g"。
擷取的群組是通過從左至右計算其開括弧來編號。例如,在運算式((A)(B(C))),有四個這樣的組:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
可以通過調用 matcher 對象的 groupCount 方法來查看錶達式有多少個分組。groupCount 方法返回一個 int 值,表示matcher對象當前有多個擷取的群組。
還有一個特殊的組(group(0)),它總是代表整個運算式。該組不包括在 groupCount 的傳回值中。
下面的例子說明如何從一個給定的字串中找到數字串:
public class RegexMatcher { public static void main(String[] args) { String line = "my name is Wangwei 300 cat."; String REGEX = "(\\D*)(\\d+)(.*)"; Pattern pattern = Pattern.compile(REGEX); Matcher m = pattern.matcher(line); if(m.find()){ System.out.println("找到值為:"+m.group(0)); System.out.println("找到值為:"+m.group(1)); System.out.println("找到值為:"+m.group(2)); }else{ System.out.println("沒有找到。。。"); } }}
以上執行個體編譯運行結果如下:
找到值為:my name is Wangwei 300 cat.找到值為:my name is Wangwei 找到值為:300
Matcher類的方法:
一.索引方法:
1.public int start()
返回以前匹配的初始索引。
2.public int start(int group)
返回在以前的匹配操作期間,由給定所捕獲的子序列的初始索引。
3.public int end()
返回最後匹配字元之後的位移量
4.public int end(int group)
返回在以前的匹配操作期間,有給定組所捕獲子序列的最後字元之後的位移量。
二.研究方法:研究方法用來檢查輸入字串並返回一個布爾值,表示是否找到該模式。
1.public boolean lookingAt()
嘗試將從地區開頭開始的輸入序列與該模式比對。
2.public boolean find()
嘗試尋找與該模式比對的輸入序列的下一個子序列。
3.public boolean find(int start)
重設此匹配器,然後嘗試尋找匹配該模式,從指定索引開始的輸入序列的下一個子序列。
4.public boolean matches()
嘗試將整個地區與模式比對
三.替換方法
1.public Matcher appendReplacement(StringBuffer sb,String replacement)
實現非終端添加和替換步驟。
2.public StringBuffer appendTail(StringBuffer sb)
實現終端添加和替換步驟.
3.public String replaceAll(String replacement)
替換模式與給定替換字串相匹配的輸入序列的每個子序列
4.public static String quoteReplacement(String s)
返回指定字串的字面替換字串。這個方法返回一個字串,就像傳遞Matcher類的appendReplacement方法一個字面字串一樣工作。
start和end方法:
public class PatternMain { public static void main(String[] args) { final String REGEX = "cat"; final String INPUT = " cat cat cat cat "; Pattern pattern = Pattern.compile(REGEX); Matcher m = pattern.matcher(INPUT); int count = 0; while(m.find()){ count++; System.out.println("match number"+count); System.out.println("start():"+m.start()); System.out.println("end():"+m.end()); } }}
編譯運行結果如下:
match number1start():1end():4match number2start():5end():8match number3start():9end():12match number4start():13end():16
可以看到這個例子是使用單詞邊界,以確保字元"c","a","t"並非僅是一個較長的詞的子串。它也提供了一些關於輸入字串中匹配發生位置的有用資訊。
Start方法返回在以前的匹配造作期間,有給定組所捕獲的子序列的初始索引,end方法最後一個匹配字元的索引加1.
matches和lookingAt方法
matches和lookingAt方法都用來嘗試一個輸入序列模式。它們的不同是matcher要求整個序列都匹配,而lookingAt不要求。這兩個方法經常在輸入字串的開始使用。
我們通過下面這個例子,來解釋這個功能:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ private static final String REGEX = "foo"; private static final String INPUT = "fooooooooooooooooo"; private static Pattern pattern; private static Matcher matcher; public static void main( String args[] ){ pattern = Pattern.compile(REGEX); matcher = pattern.matcher(INPUT); System.out.println("Current REGEX is: "+REGEX); System.out.println("Current INPUT is: "+INPUT); System.out.println("lookingAt(): "+matcher.lookingAt()); System.out.println("matches(): "+matcher.matches()); }}
以上執行個體編譯運行結果如下:
Current REGEX is: fooCurrent INPUT is: fooooooooooooooooolookingAt(): truematches(): false
replaceFirst 和 replaceAll 方法
replaceFirst 和 replaceAll 方法用來替換匹配Regex的文本。不同的是,replaceFirst 替換最初相符,replaceAll 替換所有匹配。
下面的例子來解釋這個功能:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ private static String REGEX = "dog"; private static String INPUT = "The dog says meow. " + "All dogs say meow."; private static String REPLACE = "cat"; public static void main(String[] args) { Pattern p = Pattern.compile(REGEX); // get a matcher object Matcher m = p.matcher(INPUT); INPUT = m.replaceAll(REPLACE); System.out.println(INPUT); }}
以上執行個體編譯運行結果如下:
The cat says meow. All cats say meow.
appendReplacement 和 appendTail 方法
Matcher 類也提供了appendReplacement 和 appendTail 方法用於文本替換:
看下面的例子來解釋這個功能:
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ private static String REGEX = "a*b"; private static String INPUT = "aabfooaabfooabfoob"; private static String REPLACE = "-"; public static void main(String[] args) { Pattern p = Pattern.compile(REGEX); // 擷取 matcher 對象 Matcher m = p.matcher(INPUT); StringBuffer sb = new StringBuffer(); while(m.find()){ m.appendReplacement(sb,REPLACE); } m.appendTail(sb); System.out.println(sb.toString()); }}
以上執行個體編譯運行結果如下:
-foo-foo-foo-
PatternSyntaxException 類的方法
PatternSyntaxException 是一個非強制異常類,它指示一個Regex模式中的語法錯誤。
PatternSyntaxException 類提供了下面的方法來協助我們查看發生了什麼錯誤。
1.public String getDescription()
擷取錯誤的描述
2.public int getIndex()
擷取錯誤的索引
3.public String getPattern()
擷取錯誤的Regex模式
4.public String getMseeage()
返回多行字串,包含語法錯誤及其索引的描述,錯誤的Regex模式和模式中錯誤索引的可視化指示。
JavaRegex