Java快速入門(本文篇幅較長,建議點喜歡後收藏後閱讀)
每天學會一個知識點,覺得不錯的可以留言關注下,戳我主頁獲取Java資料(工具包,面試資料,視頻教學,包含社羣解答)
Java 正則表達式
正則表達式定義了字符串的模式。
正則表達式可以用來搜索、編輯或處理文本。
正則表達式並不僅限於某一種語言,但是在每種語言中有細微的差別。
Java正則表達式和Perl的是最爲相似的。
java.util.regex包主要包括以下三個類:
- Pattern類:pattern對象是一個正則表達式的編譯表示。Pattern類沒有公共構造方法。要創建一個Pattern對象,你必須首先調用其公共靜態編譯方法,它返回一個Pattern對象。該方法接受一個正則表達式作爲它的第一個參數。
- Matcher類:Matcher對象是對輸入字符串進行解釋和匹配操作的引擎。與Pattern類一樣,Matcher也沒有公共構造方法。你需要調用Pattern對象的matcher方法來獲得一個Matcher對象。
- PatternSyntaxException:PatternSyntaxException是一個非強制異常類,它表示一個正則表達式模式中的語法錯誤。
捕獲組
捕獲組是把多個字符當一個單獨單元進行處理的方法,它通過對括號內的字符分組來創建。
例如,正則表達式(dog) 創建了單一分組,組裏包含"d","o",和"g"。
捕獲組是通過從左至右計算其開括號來編號。例如,在表達式((A)(B(C))),有四個這樣的組:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
可以通過調用matcher對象的groupCount方法來查看錶達式有多少個分組。groupCount方法返回一個int值,表示matcher對象當前有多個捕獲組。
還有一個特殊的組(組0),它總是代表整個表達式。該組不包括在groupCount的返回值中。
實例
下面的例子說明如何從一個給定的字符串中找到數字串:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
public static void main( String args[] ){
// 按指定模式在字符串查找
String line = "This order was placed for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";
// 創建 Pattern 對象
Pattern r = Pattern.compile(pattern);
// 現在創建 matcher 對象
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
} else {
System.out.println("NO MATCH");
}
}
}
以上實例編譯運行結果如下:
Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT300
Found value: 0
正則表達式語法
Matcher類的方法
索引方法
索引方法提供了有用的索引值,精確表明輸入字符串中在哪能找到匹配:
研究方法
研究方法用來檢查輸入字符串並返回一個布爾值,表示是否找到該模式:
替換方法
替換方法是替換輸入字符串裏文本的方法:
start 和end 方法
下面是一個對單詞"cat"出現在輸入字符串中出現次數進行計數的例子:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
private static final String REGEX = "\\bcat\\b";
private static final String INPUT =
"cat cat cat cattie cat";
public static void main( String args[] ){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 獲取 matcher 對象
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 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
可以看到這個例子是使用單詞邊界,以確保字母 "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: foo
Current INPUT is: fooooooooooooooooo
lookingAt(): true
matches(): false
replaceFirst 和replaceAll 方法
replaceFirst 和replaceAll 方法用來替換匹配正則表達式的文本。不同的是,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 是一個非強制異常類,它指示一個正則表達式模式中的語法錯誤。
PatternSyntaxException 類提供了下面的方法來幫助我們查看發生了什麼錯誤。