Dashboard > JAVA SE > Home > Regular Expression
JAVA SE Log In   View a printable version of the current page.
Regular Expression
Added by swanky, last edited by swanky on Jul 28, 2004
Labels: 
(None)

Java Tutorial

第一部分

正則運算式(REs)通常被錯誤地認為是只有少數人理解的一種神秘語言。
在表面上它們確實看起來雜亂無章,如果你不知道它的語法,那它的代碼在你眼中只是一堆文字垃圾而已。
實際上,正則運算式是非常簡單並且可以被理解。讀完這篇文章後,你將會通曉正則運算式的通用語法。

支援多種平臺

正則運算式最早是由數學家Stephen Kleene於1956年提出,他是在對自然語言的遞增研究成果的基礎上提出來的。
具有完整語法的正則運算式使用在字元的格式比對方面上,後來被應用到資訊技術領域。
自從那時起,正則運算式經過幾個時期的發展,現在的標準已經被ISO(國際標準組織)批准和被Open Group組織認定。

正則運算式並非一門專用語言,但它可用於在一個文件或字元查找和替代文本的一種標準。
它具有兩種標準:基本的正則運算式(BRE),擴展的正則運算式(ERE)。
ERE包括BRE功能和另外其他的概念。

許多程式中都使用了正則運算式,包括xsh,egrep,sed,vi以及在UNIX平臺下的程式。
它們可以被很多語言採納,如HTML和XML,這些採納通常只是整個標準的一個子集。

比你想象的還要普通

隨著正則運算式移植到交叉平臺的程式語言的發展,這的功能也日益完整,使用也逐漸廣泛。
網路上的搜索引擎使用它,e-mail程式也使用它,即使你不是一個UNIX程式師,你也可以使用規則語言來簡化你的程式而縮短你的開發時間。

正則運算式101

很多正則運算式的語法看起來很相似,這是因為你以前你沒有研究過它們。
通配符是RE的一個結構類型,即重復操作。讓我們先看一看ERE標準的最通用的基本語法類型。
了能夠提供具有特定用途的範例,我將使用幾個不同的程式。

第二部分

字元比對

正則運算式的關鍵之處在於確定你要搜索比對的東西,如果沒有這一概念,Res將毫無用處。

每一個運算式都包含需要查找的指令,如表A所示。

Table A: Character-matching regular expressions
格式說明:
----
操作:
解釋:
例子:
結果:
----
.
Match any one character
grep .ord sample.txt
Will match "ford", "lord", "2ord", etc. in the file sample.txt.
----
[ ]
Match any one character listed between the brackets
grep [cng]ord sample.txt
Will match only "cord", "nord", and "gord"
----
[^ ]
Match any one character not listed between the brackets

grep [^cn]ord sample.txt
Will match "lord", "2ord", etc. but not "cord" or "nord"

grep [a-zA-Z]ord sample.txt
Will match "aord", "bord", "Aord", "Bord", etc.

grep [^0-9]ord sample.txt
Will match "Aord", "aord", etc. but not "2ord", etc.

重復操作符
重復操作符,或數量詞,都描述了查找一個特定字元的次數。它們常被用於字元比對語法以查找多行的字元,可參見表B。

Table B: Regular expression repetition operators
格式說明:
----
操作:
解釋:
例子:
結果:
----
?
Match any character one time, if it exists
egrep "?erd" sample.txt
Will match "berd", "herd", etc. and "erd"
----
*
Match declared element multiple times, if it exists
egrep "n.*rd" sample.txt
Will match "nerd", "nrd", "neard", etc.
----
+
Match declared element one or more times
egrep "[n]+erd" sample.txt
Will match "nerd", "nnerd", etc., but not "erd"
----
{n}
Match declared element exactly n times
egrep "[a-z]{2}erd" sample.txt
Will match "cherd", "blerd", etc. but not "nerd", "erd", "buzzerd", etc.
----
{n,}
Match declared element at least n times
egrep ".{2,}erd" sample.txt
Will match "cherd" and "buzzerd", but not "nerd"
----
{n,N}
Match declared element at least n times, but not more than N times
egrep "n[e]{1,2}rd" sample.txt
Will match "nerd" and "neerd"

第三部分



錨是指它所要比對的格式,如圖C所示。使用它能方便你查找通用字元的合併。
例如,我用vi行編輯器命令:s來代表substitute,這一命令的基本語法是:

s/pattern_to_match/pattern_to_substitute/

Table C: Regular expression anchors
----
操作
解釋
例子
結果
----
^
Match at the beginning of a line
s/^/blah /
Inserts "blah " at the beginning of the line
----
$
Match at the end of a line
s/$/ blah/
Inserts " blah" at the end of the line
----
\<
Match at the beginning of a word
s/\</blah/
Inserts "blah" at the beginning of the word

egrep "\<blah" sample.txt
Matches "blahfield", etc.
----
\>
Match at the end of a word
s/\>/blah/
Inserts "blah" at the end of the word

egrep "\>blah" sample.txt
Matches "soupblah", etc.
----
\b
Match at the beginning or end of a word
egrep "\bblah" sample.txt
Matches "blahcake" and "countblah"
----
\B
Match in the middle of a word
egrep "\Bblah" sample.txt
Matches "sublahper", etc.

間隔

Res中的另一可便之處是間隔(或插入)符號。實際上,這一符號相當於一個OR語句並代表|符號。
下面的語句返回文件sample.txt中的"nerd" 和 "merd""""""

egrep "(n|m)erd" sample.txt

間隔功能非常強大,特別是當你尋找文件不同拼寫的時候,但你可以在下面的例子得到相同的結果:

egrep "[nm]erd" sample.txt

當你使用間隔功能與Res的高級特性連接在一起時,它的真正用處更能體現出來。

第四部分


一些保留字元
Res 的最後一個最重要特性是保留字元(也稱特定字元)。
例如,如果你想要查找"ne*rd"""ni*rd""""
格式比對語句"n[ei]*rd"" "neeeeerd" 和 "nieieierd"""""""""""""""""
因?'*'(星號)是個保留字元,你必須用一個反斜線符號來替代它,
即:"n[ei]\ *rd""""""""""""

^ (carat)
. (period)
[ (left bracket)
$ (dollar sign)
( (left parenthesis)
) (right parenthesis)
| (pipe)
* (asterisk)
+ (plus symbol)
? (question mark)
{ (left curly bracket, or left brace)
\ backslash

一旦你把以上這些字元包括在你的字元搜索中,毫無疑問Res變得非常的難讀。
比如說以下的PHP中的eregi搜索引擎代碼就很難讀了。

eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",$sendto)

你可以看到,程式的意圖很難把握。但如果你?開保留字元,你常常會錯誤地理解代碼的意思。

總結
在本文中,我們揭開了正則運算式的神秘面紗,並列出了ERE標準的通用語法。
如果你想閱覽Open Group組織的規則的完整描述,你可以參見:Regular Expressions,歡迎你在其中的討論區發表你的問題或觀點。

參考頁面:http://www.javaworld.com.tw/jute/post/view?bid=5&id=91&sty=2

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.1.5a Build:#411 Mar 16, 2006) - Bug/feature request - Contact Administrators