應(yīng)用程序常常需要有文本處理功能,比如單詞查找、電子郵件確認(rèn)或XML文檔 集成。這通常會涉及到模式匹配。Perl、sed或awk等語言通過使用正則表達式來 改善模式匹配,正則表達式是一串字符,它所定義的模式可用來查找匹配的文本。 為了使用JavaTM編程語言進行模式匹配,需 要使用帶有許多charAt子字串的StringTokenizer 類,讀取字母或符號以便處理文本。這常常導(dǎo)致復(fù)雜或凌亂的代碼。現(xiàn)在不一樣了。2平臺標(biāo)準(zhǔn)版(J2SETM)1.4版包含一個名 為java.util.regex的新軟件包,使得使用正則表達式成為可能。 目前的功能包括元字符的使用,它賦予正則表達式極大的靈活性本文概括地介紹了正則表達式的使用,并詳細(xì)解釋如何利用 java.util.regex軟件包來使用正則表達式,用以下常見情形作為
例子:§ 簡單的單詞替換 § 電子郵件確認(rèn) § 從文件中刪除控制字符 §查找文件
為了編譯這些例子中的代碼和在應(yīng)用程序中使用正則表達式,需要安裝 J2SE 1.4版。構(gòu)造正則表達式正則表達式是一種字符模式,它描述的是一組字符串。你可以使用 java.util.regex軟件包,查找、顯示或修改輸入序列中出現(xiàn)的 某個模式的一部分或全部。正則表達式最簡單的形式是一個精確的字符串,比如“Java”或 “programming”。正則表達式匹配還允許你檢查一個字符串是否符合某個具體的 句法形式,比如是不是一個電子郵件地址。為了編寫正則表達式,普通字符和特殊字符都要使用:\\$ ^ . *
+ ? [\' \']
\\.
正則表達式中出現(xiàn)的任何其他字符都是普通字符,除非它前面有個 \\。特殊字符有著特別的用處。例如,.可匹配除了換行符之外的任意字符。與 s.n這樣的正則表達式匹配的是任何三個字符的、以s 開始以n結(jié)束的字符串,包括sun和son 。在正則表達式中有許多特殊字符,可以查找一行開頭的單詞,忽略大小寫或 大小寫敏感的單詞,還有特殊字符可以給出一個范圍,比如a-e表 示從a到e的任何字母。使用這個新軟件包的正則表達式用法與Perl類似,所以如果你熟悉Perl中正則 表達式的使用,就可以在Java語言中使用同樣的表達式語法。如果你不熟悉正則 表達式,下面是一些入門的例子:構(gòu)造 匹配于
字符
x 字符 x
\\\\ 反斜線字符
\\0n 八進制值的字符0n (0 <= n <= 7)
\\0nn 八進制值的字符 0nn (0 <= n <= 7)
\\0mnn 八進制值的字符0mnn 0mnn (0 <= m <= 3, 0 <= n <= 7)
\\xhh 十六進制值的字符0xhh
\\uhhhh 十六進制值的字符0xhhhh
\\t 制表符(\'\\u0009\')
\\n 換行符 (\'\\u000A\')
\\r 回車符 (\'\\u000D\')
\\f 換頁符 (\'\\u000C\')
\\a 響鈴符 (\'\\u0007\')
\\e 轉(zhuǎn)義符 (\'\\u001B\')
\\cx T對應(yīng)于x的控制字符 x
字符類
[abc] a, b, or c (簡單類)
[^abc] 除了a、b或c之外的任意 字符(求反)
[a-zA-Z] a到z或A到Z ,包含(范圍)
[a-z-[bc]] a到z,除了b和c : [ad-z](減去)
[a-z-[m-p]] a到z,除了m到 p: [a-lq-z]
[a-z-[^def]] d, e, 或 f
預(yù)定義的字符類
. 任意字符(也許能與行終止符匹配,也許不能)
\\d 數(shù)字: [0-9]
\\D 非數(shù)字: [^0-9]
\\s 空格符: [ \\t\\n\\x0B\\f\\r]
\\S 非空格符: [^\\s]
\\w 單詞字符: [a-zA-Z_0-9]
\\W 非單詞字符: [^\\w]
有關(guān)進一步的詳情和例子,請參閱 Pattern類的文檔。
類和方法
下面的類根據(jù)正則表達式指定的模式,與字符序列進行匹配。
Pattern類
Pattern類的實例表示以字符串形式指定的正則表達式,其語 法類似于Perl所用的語法。
用字符串形式指定的正則表達式,必須先編譯成Pattern類的 實例。生成的模式用于創(chuàng)建Matcher對象,它根據(jù)正則表達式與任 意字符序列進行匹配。多個匹配器可以共享一個模式,因為它是非專屬的。
用compile方法把給定的正則表達式編譯成模式,然后用 matcher方法創(chuàng)建一個匹配器,這個匹配器將根據(jù)此模式對給定輸 入進行匹配。pattern 方法可返回編譯這個模式所用的正則表達 式。
split方法是一種方便的方法,它在與此模式匹配的位置將給 定輸入序列切分開。下面的例子演示了:
/*
* 用split對以逗號和/或空格分隔的輸入字符串進行切分。
*/
import java.util.regex.*;
public class Splitter {
public static void main(String[] args) throws Exception {
// Create a pattern to match breaks
Pattern p = Pattern.compile(\"[,\\\\s]+\");
// Split input with the pattern
String[] result =
p.split(\"one,two, three four , five\");
for (int i=0; i<result.length; i++)
System.out.println(result[i]);
}
}
Matcher類
Matcher類的實例用于根據(jù)給定的字符串序列模式,對字符序 列進行匹配。使用CharSequence接口把輸入提供給匹配器,以便 支持來自多種多樣輸入源的字符的匹配。
通過調(diào)用某個模式的matcher方法,從這個模式生成匹配器。 匹配器創(chuàng)建之后,就可以用它來執(zhí)行三類不同的匹配操作:
§ matches方法試圖根據(jù)此模式,對整個輸入序列進行匹配。
§ lookingAt方法試圖根據(jù)此模式,從開始處對輸入序列進 行匹配。
§ find方法將掃描輸入序列,尋找下一個與模式匹配的地方。
這些方法都會返回一個表示成功或失敗的布爾值。如果匹配成功,通過查詢 匹配器的狀態(tài),可以獲得更多的信息
這個類還定義了用新字符串替換匹配序列的方法,這些字符串的內(nèi)容如果需 要的話,可以從匹配結(jié)果推算得出。
appendReplacement方法先添加字符串中從當(dāng)前位置到下一個 匹配位置之間的所有字符,然后添加替換值。appendTail添加的 是字符串中從最后一次匹配的位置之后開始,直到結(jié)尾的部分。
例如,在字符串blahcatblahcatblah中,第一個 appendReplacement添加blahdog。第二個 appendReplacement添加blahdog,然后 appendTail添加blah,就生成了: blahdogblahdogblah。請參見示例 簡單的單詞替換。
CharSequence接口
CharSequence接口為許多不同類型的字符序列提供了統(tǒng)一的只 讀訪問。你提供要從不同來源搜索的數(shù)據(jù)。用String, StringBuffer 和CharBuffer實現(xiàn)CharSequence,,這樣就可以很 容易地從它們那里獲得要搜索的數(shù)據(jù)。如果這些可用數(shù)據(jù)源沒一個合適的,你可 以通過實現(xiàn)CharSequence接口,編寫你自己的輸入源。
Regex情景范例
以下代碼范例演示了java.util.regex軟件包在各種常見情形 下的用法:
簡單的單詞替換
/*
* This code writes \"One dog, two dogs in the yard.\"
* to the standard-output stream:
*/
import java.util.regex.*;
public class Replacement {
public static void main(String[] args)
throws Exception {
// Create a pattern to match cat
Pattern p = Pattern.compile(\"cat\");
// Create a matcher with an input string
Matcher m = p.matcher(\"one cat,\" +
\" two cats in the yard\");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
// Loop through and create a new String
// with the replacements
while(result) {
m.appendReplacement(sb, \"dog\");
result = m.find();
}
// Add the last segment of input to
// the new String
m.appendTail(sb);
System.out.println(sb.toString());
}
}
電子郵件確認(rèn)
以下代碼是這樣一個例子:你可以檢查一些字符是不是一個電子郵件地址。 它并不是一個完整的、適用于所有可能情形的電子郵件確認(rèn)程序,但是可以在 需要時加上它。
/*
* Checks for invalid characters
* in email addresses
*/
public class EmailValidation {
public static void main(String[] args)
throws Exception {
String input = \"@sun.com\";
//Checks for email addresses starting with
//inappropriate symbols like dots or @ signs.
Pattern p = Pattern.compile(\"^\\\\.|^\\\\@\");
Matcher m = p.matcher(input);
if (m.find())
System.err.println(\"Email addresses don\'t start\" +
\" with dots or @ signs.\");
//Checks for email addresses that start with
//www. and prints a message if it does.
p = Pattern.compile(\"^www\\\\.\");
m = p.matcher(input);
if (m.find()) {
System.out.println(\"Email addresses don\'t start\" +
\" with \\\"www.\\\", only web pages do.\");
}
p = Pattern.compile(\"[^A-Za-z0-9\\\\.\\\\@_\\\\-~#]+\");
m = p.matcher(input);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result) {
deletedIllegalChars = true;
m.appendReplacement(sb, \"\");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
input = sb.toString();
if (deletedIllegalChars) {
System.out.println(\"It contained incorrect characters\" +
\" , such as spaces or commas.\");
}
}
}
從文件中刪除控制字符
/* This class removes control characters from a named
* file.
*/
import java.util.regex.*;
import java.io.*;
public class Control {
public static void main(String[] args)
throws Exception {
//Create a file object with the file name
//in the argument:
File fin = new File(\"fileName1\");
File fout = new File(\"fileName2\");
//Open and input and output stream
FileInputStream fis =
new FileInputStream(fin);
FileOutputStream fos =
new FileOutputStream(fout);
BufferedReader in = new BufferedReader(
new InputStreamReader(fis));
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(fos));
// The pattern matches control characters
Pattern p = Pattern.compile(\"{cntrl}\");
Matcher m = p.matcher(\"\");
String aLine = null;
while((aLine = in.readLine()) != null) {
m.reset(aLine);
//Replaces control characters with an empty
//string.
String result = m.replaceAll(\"\");
out.write(result);
out.newLine();
}
in.close();
out.close();
}
}
文件查找
/*
* Prints out the comments found in a .java file.
*/
import java.util.regex.*;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.nio.channels.*;
public class CharBufferExample {
public static void main(String[] args) throws Exception {
// Create a pattern to match comments
Pattern p =
Pattern.compile(\"http://.*$\", Pattern.MULTILINE);
// Get a Channel for the source file
File f = new File(\"Replacement.java\");
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();
// Get a CharBuffer from the source file
ByteBuffer bb =
fc.map(FileChannel.MAP_RO, 0, (int)fc.size());
Charset cs = Charset.forName(\"8859_1\");
CharsetDecoder cd = cs.newDecoder();
CharBuffer cb = cd.decode(bb);
// Run some matches
Matcher m = p.matcher(cb);
while (m.find())
System.out.println(\"Found comment: \"+m.group());
}
}
結(jié)論
現(xiàn)在Java編程語言中的模式匹配和許多其他編程語言一樣靈活了。可以在應(yīng) 用程序中使用正則表達式,確保數(shù)據(jù)在輸入數(shù)據(jù)庫或發(fā)送給應(yīng)用程序其他部分之 前,格式是正確的,正則表達式還可以用于各種各樣的管理性工作。簡而言之, 在Java編程中,可以在任何需要模式匹配的地方使用正則表達式。
例子:§ 簡單的單詞替換 § 電子郵件確認(rèn) § 從文件中刪除控制字符 §查找文件
為了編譯這些例子中的代碼和在應(yīng)用程序中使用正則表達式,需要安裝 J2SE 1.4版。構(gòu)造正則表達式正則表達式是一種字符模式,它描述的是一組字符串。你可以使用 java.util.regex軟件包,查找、顯示或修改輸入序列中出現(xiàn)的 某個模式的一部分或全部。正則表達式最簡單的形式是一個精確的字符串,比如“Java”或 “programming”。正則表達式匹配還允許你檢查一個字符串是否符合某個具體的 句法形式,比如是不是一個電子郵件地址。為了編寫正則表達式,普通字符和特殊字符都要使用:\\$ ^ . *
+ ? [\' \']
\\.
正則表達式中出現(xiàn)的任何其他字符都是普通字符,除非它前面有個 \\。特殊字符有著特別的用處。例如,.可匹配除了換行符之外的任意字符。與 s.n這樣的正則表達式匹配的是任何三個字符的、以s 開始以n結(jié)束的字符串,包括sun和son 。在正則表達式中有許多特殊字符,可以查找一行開頭的單詞,忽略大小寫或 大小寫敏感的單詞,還有特殊字符可以給出一個范圍,比如a-e表 示從a到e的任何字母。使用這個新軟件包的正則表達式用法與Perl類似,所以如果你熟悉Perl中正則 表達式的使用,就可以在Java語言中使用同樣的表達式語法。如果你不熟悉正則 表達式,下面是一些入門的例子:構(gòu)造 匹配于
字符
x 字符 x
\\\\ 反斜線字符
\\0n 八進制值的字符0n (0 <= n <= 7)
\\0nn 八進制值的字符 0nn (0 <= n <= 7)
\\0mnn 八進制值的字符0mnn 0mnn (0 <= m <= 3, 0 <= n <= 7)
\\xhh 十六進制值的字符0xhh
\\uhhhh 十六進制值的字符0xhhhh
\\t 制表符(\'\\u0009\')
\\n 換行符 (\'\\u000A\')
\\r 回車符 (\'\\u000D\')
\\f 換頁符 (\'\\u000C\')
\\a 響鈴符 (\'\\u0007\')
\\e 轉(zhuǎn)義符 (\'\\u001B\')
\\cx T對應(yīng)于x的控制字符 x
字符類
[abc] a, b, or c (簡單類)
[^abc] 除了a、b或c之外的任意 字符(求反)
[a-zA-Z] a到z或A到Z ,包含(范圍)
[a-z-[bc]] a到z,除了b和c : [ad-z](減去)
[a-z-[m-p]] a到z,除了m到 p: [a-lq-z]
[a-z-[^def]] d, e, 或 f
預(yù)定義的字符類
. 任意字符(也許能與行終止符匹配,也許不能)
\\d 數(shù)字: [0-9]
\\D 非數(shù)字: [^0-9]
\\s 空格符: [ \\t\\n\\x0B\\f\\r]
\\S 非空格符: [^\\s]
\\w 單詞字符: [a-zA-Z_0-9]
\\W 非單詞字符: [^\\w]
有關(guān)進一步的詳情和例子,請參閱 Pattern類的文檔。
類和方法
下面的類根據(jù)正則表達式指定的模式,與字符序列進行匹配。
Pattern類
Pattern類的實例表示以字符串形式指定的正則表達式,其語 法類似于Perl所用的語法。
用字符串形式指定的正則表達式,必須先編譯成Pattern類的 實例。生成的模式用于創(chuàng)建Matcher對象,它根據(jù)正則表達式與任 意字符序列進行匹配。多個匹配器可以共享一個模式,因為它是非專屬的。
用compile方法把給定的正則表達式編譯成模式,然后用 matcher方法創(chuàng)建一個匹配器,這個匹配器將根據(jù)此模式對給定輸 入進行匹配。pattern 方法可返回編譯這個模式所用的正則表達 式。
split方法是一種方便的方法,它在與此模式匹配的位置將給 定輸入序列切分開。下面的例子演示了:
/*
* 用split對以逗號和/或空格分隔的輸入字符串進行切分。
*/
import java.util.regex.*;
public class Splitter {
public static void main(String[] args) throws Exception {
// Create a pattern to match breaks
Pattern p = Pattern.compile(\"[,\\\\s]+\");
// Split input with the pattern
String[] result =
p.split(\"one,two, three four , five\");
for (int i=0; i<result.length; i++)
System.out.println(result[i]);
}
}
Matcher類
Matcher類的實例用于根據(jù)給定的字符串序列模式,對字符序 列進行匹配。使用CharSequence接口把輸入提供給匹配器,以便 支持來自多種多樣輸入源的字符的匹配。
通過調(diào)用某個模式的matcher方法,從這個模式生成匹配器。 匹配器創(chuàng)建之后,就可以用它來執(zhí)行三類不同的匹配操作:
§ matches方法試圖根據(jù)此模式,對整個輸入序列進行匹配。
§ lookingAt方法試圖根據(jù)此模式,從開始處對輸入序列進 行匹配。
§ find方法將掃描輸入序列,尋找下一個與模式匹配的地方。
這些方法都會返回一個表示成功或失敗的布爾值。如果匹配成功,通過查詢 匹配器的狀態(tài),可以獲得更多的信息
這個類還定義了用新字符串替換匹配序列的方法,這些字符串的內(nèi)容如果需 要的話,可以從匹配結(jié)果推算得出。
appendReplacement方法先添加字符串中從當(dāng)前位置到下一個 匹配位置之間的所有字符,然后添加替換值。appendTail添加的 是字符串中從最后一次匹配的位置之后開始,直到結(jié)尾的部分。
例如,在字符串blahcatblahcatblah中,第一個 appendReplacement添加blahdog。第二個 appendReplacement添加blahdog,然后 appendTail添加blah,就生成了: blahdogblahdogblah。請參見示例 簡單的單詞替換。
CharSequence接口
CharSequence接口為許多不同類型的字符序列提供了統(tǒng)一的只 讀訪問。你提供要從不同來源搜索的數(shù)據(jù)。用String, StringBuffer 和CharBuffer實現(xiàn)CharSequence,,這樣就可以很 容易地從它們那里獲得要搜索的數(shù)據(jù)。如果這些可用數(shù)據(jù)源沒一個合適的,你可 以通過實現(xiàn)CharSequence接口,編寫你自己的輸入源。
Regex情景范例
以下代碼范例演示了java.util.regex軟件包在各種常見情形 下的用法:
簡單的單詞替換
/*
* This code writes \"One dog, two dogs in the yard.\"
* to the standard-output stream:
*/
import java.util.regex.*;
public class Replacement {
public static void main(String[] args)
throws Exception {
// Create a pattern to match cat
Pattern p = Pattern.compile(\"cat\");
// Create a matcher with an input string
Matcher m = p.matcher(\"one cat,\" +
\" two cats in the yard\");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
// Loop through and create a new String
// with the replacements
while(result) {
m.appendReplacement(sb, \"dog\");
result = m.find();
}
// Add the last segment of input to
// the new String
m.appendTail(sb);
System.out.println(sb.toString());
}
}
電子郵件確認(rèn)
以下代碼是這樣一個例子:你可以檢查一些字符是不是一個電子郵件地址。 它并不是一個完整的、適用于所有可能情形的電子郵件確認(rèn)程序,但是可以在 需要時加上它。
/*
* Checks for invalid characters
* in email addresses
*/
public class EmailValidation {
public static void main(String[] args)
throws Exception {
String input = \"@sun.com\";
//Checks for email addresses starting with
//inappropriate symbols like dots or @ signs.
Pattern p = Pattern.compile(\"^\\\\.|^\\\\@\");
Matcher m = p.matcher(input);
if (m.find())
System.err.println(\"Email addresses don\'t start\" +
\" with dots or @ signs.\");
//Checks for email addresses that start with
//www. and prints a message if it does.
p = Pattern.compile(\"^www\\\\.\");
m = p.matcher(input);
if (m.find()) {
System.out.println(\"Email addresses don\'t start\" +
\" with \\\"www.\\\", only web pages do.\");
}
p = Pattern.compile(\"[^A-Za-z0-9\\\\.\\\\@_\\\\-~#]+\");
m = p.matcher(input);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result) {
deletedIllegalChars = true;
m.appendReplacement(sb, \"\");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
input = sb.toString();
if (deletedIllegalChars) {
System.out.println(\"It contained incorrect characters\" +
\" , such as spaces or commas.\");
}
}
}
從文件中刪除控制字符
/* This class removes control characters from a named
* file.
*/
import java.util.regex.*;
import java.io.*;
public class Control {
public static void main(String[] args)
throws Exception {
//Create a file object with the file name
//in the argument:
File fin = new File(\"fileName1\");
File fout = new File(\"fileName2\");
//Open and input and output stream
FileInputStream fis =
new FileInputStream(fin);
FileOutputStream fos =
new FileOutputStream(fout);
BufferedReader in = new BufferedReader(
new InputStreamReader(fis));
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(fos));
// The pattern matches control characters
Pattern p = Pattern.compile(\"{cntrl}\");
Matcher m = p.matcher(\"\");
String aLine = null;
while((aLine = in.readLine()) != null) {
m.reset(aLine);
//Replaces control characters with an empty
//string.
String result = m.replaceAll(\"\");
out.write(result);
out.newLine();
}
in.close();
out.close();
}
}
文件查找
/*
* Prints out the comments found in a .java file.
*/
import java.util.regex.*;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.nio.channels.*;
public class CharBufferExample {
public static void main(String[] args) throws Exception {
// Create a pattern to match comments
Pattern p =
Pattern.compile(\"http://.*$\", Pattern.MULTILINE);
// Get a Channel for the source file
File f = new File(\"Replacement.java\");
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();
// Get a CharBuffer from the source file
ByteBuffer bb =
fc.map(FileChannel.MAP_RO, 0, (int)fc.size());
Charset cs = Charset.forName(\"8859_1\");
CharsetDecoder cd = cs.newDecoder();
CharBuffer cb = cd.decode(bb);
// Run some matches
Matcher m = p.matcher(cb);
while (m.find())
System.out.println(\"Found comment: \"+m.group());
}
}
結(jié)論
現(xiàn)在Java編程語言中的模式匹配和許多其他編程語言一樣靈活了。可以在應(yīng) 用程序中使用正則表達式,確保數(shù)據(jù)在輸入數(shù)據(jù)庫或發(fā)送給應(yīng)用程序其他部分之 前,格式是正確的,正則表達式還可以用于各種各樣的管理性工作。簡而言之, 在Java編程中,可以在任何需要模式匹配的地方使用正則表達式。