在String類中,有四個特殊的方法:
public String[] split(String regex)
- 根據給定的正則表達式的匹配來拆分此字符串。
該方法的作用就像是使用給定的表達式和限制參數 0 來調用兩參數 split
方法。因此,結果數組中不包括結尾空字符串。
例如,字符串 "boo:and:foo" 產生帶有下面這些表達式的結果:
Regex |
結果 |
: |
{ "boo", "and", "foo" } |
o |
{ "b", "", ":and:f" } |
-
- 參數:
regex
- 定界正則表達式
- 返回: 字符串數組,根據給定正則表達式的匹配來拆分此字符串,從而生成此數組。
- 拋出:
PatternSyntaxException
- 如果正則表達式的語法無效
public String[] split(String regex, int limit)
- 根據匹配給定的正則表達式來拆分此字符串。
此方法返回的數組包含此字符串的每個子字符串,這些子字符串由另一個匹配給定的表達式的子字符串終止或由字符串結束來終止。數組中的子字符串按它們在此字符串中的順序排列。如果表達式不匹配輸入的任何部分,則結果數組只具有一個元素,即此字符串。
limit 參數控制模式應用的次數,因此影響結果數組的長度。如果該限制 n 大于 0,則模式將被最多應用 n - 1 次,數組的長度將不會大于 n,而且數組的最后項將包含超出最后匹配的定界符的所有輸入。如果 n 為非正,則模式將被應用盡可能多的次數,而且數組可以是任意長度。如果 n 為零,則模式將被應用盡可能多的次數,數組可有任何長度,并且結尾空字符串將被丟棄。
例如,字符串 "boo:and:foo" 使用這些參數可生成下列結果:
Regex |
Limit |
結果 |
: |
2 |
{ "boo", "and:foo" } |
: |
5 |
{ "boo", "and", "foo" } |
: |
-2 |
{ "boo", "and", "foo" } |
o |
5 |
{ "b", "", ":and:f", "", "" } |
o |
-2 |
{ "b", "", ":and:f", "", "" } |
o |
0 |
{ "b", "", ":and:f" } |
這種形式的方法調用 str.split(regex, n) 產生與以下表達式完全相同的結果:
Pattern
.compile
(regex).split
(str, n)
參數: regex
- 定界正則表達式 ;limit
- 結果閾值,如上所述
- 返回: 字符串數組,根據給定正則表達式的匹配來拆分此字符串,從而生成此數組
public String replaceAll(String regex, String replacement)
- 使用給定的 replacement 字符串替換此字符串匹配給定的正則表達式的每個子字符串。
此方法調用的 str.replaceAll(regex, repl) 形式產生與以下表達式完全相同的結果:
Pattern
.compile
(regex).matcher
(str).replaceAll
(repl)
參數: regex
- 用來匹配此字符串的正則表達式
- 返回: 得到的 String
public String replaceFirst(String regex, String replacement)
- 使用給定的 replacement 字符串替換此字符串匹配給定的正則表達式的第一個子字符串。
此方法調用的 str.replaceFirst(regex, repl) 形式產生與以下表達式完全相同的結果:
Pattern
.compile
(regex).matcher
(str).replaceFirst
(repl)
參數:regex
- 用來匹配此字符串的正則表達式
- 返回: 得到的 String
這四個方法中都有一個參數為正則表達式(Regular Expression),而不是普通的字符串。
在正則表達式中具有特殊含義的字符
特殊字符
|
描述
|
. |
表示任意一個字符 |
[abc] |
表示a、b或c中的任意一個字符 |
[^abc] |
除a、b和c以外的任意一個字符 |
[a-zA-z] |
介于a到z,或A到Z中的任意一個字符 |
\s |
空白符(空格、tab、換行、換頁、回車) |
\S |
非空白符 |
\d |
任意一個數字[0-9] |
\D |
任意一個非數字[^0-9] |
\w |
詞字符[a-zA-Z_0-9] |
\W |
非詞字符 |
表示字符出現次數的符號
表示次數的符號
|
描述
|
* |
0 次或者多次 |
+ |
1 次或者多次 |
? |
0 次或者 1 次 |
{n} |
恰好 n 次 |
{n, m} |
至少 n 次,不多于 m 次 |
public class RegDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
// 例如,字符串 "boo:and:foo" 產生帶有下面這些表達式的結果: Regex 結果
// : { "boo", "and", "foo" }
// o { "b", "", ":and:f" }
String tempStr = "boo:and:foo";
String[] a = tempStr.split(":");
pringStringArray(a);
String[] b = tempStr.split("o");
pringStringArray(b);
System.out.println("--------------------------");
// Regex Limit 結果
// : 2 { "boo", "and:foo" }
// : 5 { "boo", "and", "foo" }
// : -2 { "boo", "and", "foo" }
// o 5 { "b", "", ":and:f", "", "" }
// o -2 { "b", "", ":and:f", "", "" }
// o 0 { "b", "", ":and:f" }
pringStringArray(tempStr.split(":", 2));
pringStringArray(tempStr.split(":", 5));
pringStringArray(tempStr.split(":", -2));
pringStringArray(tempStr.split("o", 5));
pringStringArray(tempStr.split("o", -2));
pringStringArray(tempStr.split("o", 0));
// 字符串 "boo:and:foo"中的所有“:”都被替換為“XX”,輸出:booXXandXXfoo
System.out.println(tempStr.replaceAll(":", "XX"));
// 字符串 "boo:and:foo"中的第一個“:”都被替換為“XX”,輸出: booXXand:foo
System.out.println(tempStr.replaceFirst(":", "XX"));
}
public static void pringStringArray(String[] s) {
int index = s.length;
for (int i = 0; i < index; i++) {
System.err.println(i + ": " + s[i]);
}
}
}
下面的程序演示了正則表達式的用法:
/**
* discription:
*
* @author CoderDream
*
*/
public class RegularExTester {
/**
* @param args
*/
public static void main(String[] args) {
// 把字符串中的“aaa”全部替換為“z”,打印:zbzcz
System.out.println("aaabaaacaaa".replaceAll("a{3}", "z"));
// 把字符串中的“aaa”、“aa”或者“a”全部替換為“*”,打印:*b*c*
System.out.println("aaabaaca".replaceAll("a{1,3}", "\\*"));
// 把字符串中的數字全部替換為“z”,打印:zzzazzbzzcc
System.out.println("123a44b35cc".replaceAll("\\d", "z"));
// 把字符串中的非數字全部替換為“0”,打印:1234000435000
System.out.println("1234abc435def".replaceAll("\\D", "0"));
// 把字符串中的“.”全部替換為“\”,打印:com\abc\dollapp\Doll
System.out.println("com.abc.dollapp.Doll".replaceAll("\\.", "\\\\"));
// 把字符串中的“a.b”全部替換為“_”,
// “a.b”表示長度為3的字符串,以“a”開頭,以“b”結尾
// 打印:-hello-all
System.out.println("azbhelloahball".replaceAll("a.b", "-"));
// 把字符串中的所有詞字符替換為“#”
// 正則表達式“[a-zA-z_0-9]”等價于“\w”
// 打印:#.#.#.#.#.#
System.out.println("a.b.c.1.2.3.4".replaceAll("[a-zA-z_0-9]", "#"));
System.out.println("a.b.c.1.2.3.4".replaceAll("\\w", "#"));
}
}
值得注意的是,由于“.”、“?”和“*”等在正則表達式中具有特殊的含義,如果要表示字面上的這些字符,必須以“\\”開頭。例如為了把字符串“com.abc.dollapp.Doll”中的“.”替換為“\”,應該調用replaceAll("\\.",
\\\\)方法。
Java中的正則表達式類
public interface MatchResult
匹配操作的結果。
此接口包含用于確定與正則表達式匹配結果的查詢方法。通過 MatchResult
可以查看匹配邊界、組和組邊界,但是不能修改
public final class Matcher
-
- extends Object
- implements MatchResult
通過解釋 Pattern
對
字符序列
執行匹配操作的引擎。
通過調用模式的 matcher
方法從模式創建匹配器。創建匹配器后,可以使用它執行三種不同的匹配操作:
每個方法都返回一個表示成功或失敗的布爾值。通過查詢匹配器的狀態可以獲取關于成功匹配的更多信息。
public final class Pattern
-
- extends Object
- implements Serializable
正則表達式的編譯表示形式。
指定為字符串的正則表達式必須首先被編譯為此類的實例。然后,可將得到的模式用于創建 Matcher
對象,依照正則表達式,該對象可以與任意
字符序列
匹配。執行匹配所涉及的所有狀態都駐留在匹配器中,所以多個匹配器可以共享同一模式。
因此,典型的調用順序是
Pattern p = Pattern.matches
();
在僅使用一次正則表達式時,可以方便地通過此類定義 matches
方法。此方法編譯表達式并在單個調用中將輸入序列與其匹配。語句
boolean b = Pattern.matches("a*b", "aaaaab");
等效于上面的三個語句,盡管對于重復的匹配而言它效率不高,因為它不允許重用已編譯的模式。
此類的實例是不可變的,可供多個并發線程安全使用。Matcher
類的實例用于此目的則不安全。
測試代碼:
/**
* discription:Java中正則表達式類的使用
*
* @author CoderDream
*
*/
public class RegDemo {
/**
* @param args
*/
public static void main(String[] args) {
// 檢查字符串中是否含有“aaa”,有返回:true,無返回:false
System.out.println(isHaveBeenSetting("a{3}", "aaabaaacaaa"));
System.out.println(isHaveBeenSetting("a{3}", "aab"));
// 把字符串“abbaaacbaaaab”中的“aaa”全部替換為“z”,打印:abbzbza
System.out.println(replaceStr("a{3}", "abbaaabaaaa", "z"));
}
/**
*
* @param regEx
* 設定的正則表達式
* @param tempStr
* 系統參數中的設定的字符串
* @return 是否系統參數中的設定的字符串含有設定的正則表達式 如果有的則返回true
*/
public static boolean isHaveBeenSetting(String regEx, String tempStr) {
boolean result = false;
try {
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(tempStr);
result = m.find();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 將字符串含有的regEx表達式替換為replaceRegEx
*
* @param regEx
* 需要被替換的正則表達式
* @param tempStr
* 替換的字符串
* @param replaceRegEx
* 替換的正則表達式
* @return 替換好后的字符串
*/
public static String replaceStr(String regEx, String tempStr,
String replaceRegEx) {
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(tempStr);
tempStr = m.replaceAll(replaceRegEx);
return tempStr;
}
}
posted on 2008-02-28 11:40
CoderDream 閱讀(2345)
評論(0) 編輯 收藏 所屬分類:
學習筆記