JDK1.4之正規表示式

來源:互聯網
上載者:User
 什麼是正規表示式呢(Reqular Expressions)

就是針對檔案、字串,透過一種很特別的表示式來作search與replace

因為在unix上有很多系統設定都是存放在文字檔中,因此網管或程式設計常常需要作搜尋與取代

所以發展出一種特殊的命令叫做正規表示式

我們可以很簡單的用 "s/</lt;/g" 這個正規式將字串中所有含有"<"的字元轉換成"lt;"

因此jdk1.4提供了一組正規表示式的package供大家使用

若是jdk1.4以下的可以到http://jakarta.apache.org/oro取得相關功能的package

剛剛列出的一串符號" s/</lt;/g" 就是正規語法,所以請先瞭解正規的表示式

適用於j2sdk1.4的正規語法

"." 代表任何字元

正規式 原字串 符合之字串
. ab a
.. abc ab

"+" 代表一個或以個以上的字元
"*" 代表零個或是零個以上的字元

正規式 原字串 符合之字串
+ ab ab
* abc abc

"( )"群組

正規式 原字串 符合之字串
(ab)* aabab abab

字元類

正規式 原字串 符合之字串
[a-dA-D0-9]* abczA0 abcA0
[^a-d]* abe0 e0
[a-d]* abcdefgh abab

簡式

/d 等於 [0-9] 數字
/D 等於 [^0-9] 非數字
/s 等於 [ /t/n/x0B/f/r] 空白字元
/S 等於 [^ /t/n/x0B/f/r] 非空白字元
/w 等於 [a-zA-Z_0-9] 數字或是英文字
/W 等於 [^a-zA-Z_0-9] 非數字與英文字

每一行的開頭或結尾

^ 表示每行的開頭
$ 表示每行的結尾

--------------------------------------------------------------------------------

正規表示式 java.util.regex 相關的類別

Pattern—正規表示式的類別
Matcher—經過正規化的結果
PatternSyntaxExpression—Exception thrown while attempting to compile a regular expression

範例1: 將字串中所有符合"<"的字元取代成"lt;"

import java.io.*;
import java.util.regex.*;
/**
* 將字串中所有符合"<"的字元取代成"lt;"
*/
public static void replace01(){
// BufferedReader lets us read line-by-line
Reader r = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( r );
Pattern pattern = Pattern.compile( "<" ); // 搜尋某字串所有符合'<'的字元
try{
while (true) {
String line = br.readLine();
// Null line means input is exhausted
if (line==null)
break;
Matcher a = pattern.matcher(line);
while(a.find()){
System.out.println("搜尋到的字元是" + a.group());
}
System.out.println(a.replaceAll("lt;"));// 將所有符合字元取代成lt;
}
}catch(Exception ex){ex.printStackTrace();};
}

範例2:

import java.io.*;
import java.util.regex.*;
/**
* 類似StringTokenizer的功能
* 將字串以","分隔然後比對哪個token最長
*/
public static void search01(){
// BufferedReader lets us read line-by-line
Reader r = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( r );
Pattern pattern = Pattern.compile( ",//s*" );// 搜尋某字串所有","的字元
try{
while (true) {
String line = br.readLine();
String words[] = pattern.split(line);
// Null line means input is exhausted
if (line==null)
break;
// -1 means we haven't found a word yet
int longest=-1;
int longestLength=0;
for (int i=0; i<words.length; ++i) {
System.out.println("分段:" + words[i] );
if (words[i].length() > longestLength) {
longest = i;
longestLength = words[i].length();
}
}
System.out.println( "長度最長為:" + words[longest] );
}
}catch(Exception ex){ex.printStackTrace();};
}

--------------------------------------------------------------------------------

其他的正規語法

/^/s* # 忽略每行開始的空白字元
(M(s|r|rs)/.) # 符合 Ms., Mrs., and Mr. (titles)

--------------------------------------------------------------------------------

參考資料

http://java.sun.com/

http://www.javalobby.org/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.