?? java.regex.tutorial.html
字號:
if (!found) {
System.out.printf("No match found.%n");
}
}
}
}</pre>
JDK 1.4 適用的測試用具(<a href="src/RegexTestHarnessV4.java">RegexTestHarnessV4.java</a>):<br/>
<pre name="java" id="java">import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTestHarnessV4 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(new BufferedInputStream(System.in))
);
while (true) {
System.out.print("\nEnter your regex: ");
Pattern pattern = Pattern.compile(br.readLine());
System.out.print("Enter input string to search: ");
Matcher matcher = pattern.matcher(br.readLine());
boolean found = false;
while (matcher.find()) {
System.out.println("I found the text \"" + matcher.group() +
"\" starting at index " + matcher.start() +
" and ending at index " + matcher.end() +
".");
found = true;
}
if (!found) {
System.out.println("No match found.");
}
}
}
}</pre>
<div id="h2"><a name="reg2"></a>2 字符串<span class="returnContents"><a href="#contents">返回目錄</a></span></div>
在大多數(shù)的情況下,API所支持模式匹配的基本形式是匹配字符串,如果正則表達式是<code>foo</code>,輸入的字符串也是 foo,這個匹配將會是成功的,因為這兩個字符串是相同的。試著用測試用具來測試一下:
<pre id="console">Enter your regex: foo
Enter input string to search: foo
I found the text "foo" starting at index 0 and ending at index 3.</pre>
結(jié)果確實是成功的。注意當輸入的字符串是 3 個字符長度的時候,開始的索引是 0,結(jié)束的索引是 3。這個是約定俗成的,范圍包括開始的索引,不包括結(jié)束的索引,如下圖所示:<br/>
<div class="picSpec">
<img src="resource/regex3-1.gif" align="center"><br/>
圖 1 字符串“foo”的單元格編號和索引值<a name="note_04"></a><sup><a href="#note04">[4]</a></sup></div>
字符串中的每一個字符位于其自身的<em>單元格</em>(cell)中,在每個單元格之間有索引指示位。字符串“foo”始于索引 0 處,止于索引 3 處,即使是這些字符它們自己僅占據(jù)了 0、1 和 2 號單元格。<br/>
就子序列匹配而言,你會注意到一些重疊,下一次匹配開始索引與前一次匹配的結(jié)束索引是相同的:<br/>
<pre id="console">Enter your regex: foo
Enter input string to search: foofoofoo
I found the text "foo" starting at index 0 and ending at index 3.
I found the text "foo" starting at index 3 and ending at index 6.
I found the text "foo" starting at index 6 and ending at index 9.</pre>
<div id="h3"><a name="reg2_1"></a>2.1 元字符<span class="returnContents"><a href="#contents">返回目錄</a></span></div>
API 也支持許多可以影響模式匹配的特殊字符。把正則表達式改為<code>cat.</code>并輸入字符串“cats”,輸出如下所示:
<pre id="console">Enter your regex: cat.
Enter input string to search: cats
I found the text "cats" starting at index 0 and ending at index 4.</pre>
雖然在輸入的字符串中沒有點(.),但這個匹配仍然是成功的。這是由于點(<code>.</code>)是一個<em>元字符</em>(metacharacters)(被這個匹配翻譯成了具有特殊意義的字符了)。這個例子為什么能匹配成功的原因在于,元字符<code>.</code>指的是“任意字符”。<br/>
API 所支持的元字符有:<code>(</code><code>[</code><code>{</code><code>\</code><code>^</code><code>-</code><code>$</code><code>|</code><code>}</code><code>]</code><code>)</code><code>?</code><code>*</code><code>+</code><code>.</code>
<p id="tip">
注意:在學習過更多的如何構(gòu)建正則表達式后,你會碰到這些情況:上面的這些特殊字符不應該被處理為元字符。然而也能夠使用這個清單來檢查一個特殊的字符是否會被認為是元字符。例如,字符 !、@ 和 # 決不會有特殊的意義。
</p>
有兩種方法可以強制將元字符處理成為普通字符:<br/>
1. 在元字符前加上反斜線(<code>\</code>);<br/>
2. 把它放在<code>\Q</code>(引用開始)和<code>\E</code>(引用結(jié)束)之間<a name="note_05"></a><sup><a href="#note05">[5]</a></sup>。在使用這種技術(shù)時,<code>\Q</code>和<code>\E</code>能被放于表達式中的任何位置(假設(shè)先出現(xiàn)<code>\Q</code><a name="note_06"></a><sup><a href="#note06">[6]</a></sup>)<br/>
<div id="h2"><a name="reg3"></a>3 字符類<span class="returnContents"><a href="#contents">返回目錄</a></span></div>
如果你曾看過 <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html" target="_blank">Pattern</a> 類的說明,會看到一些構(gòu)建正則表達式的概述。在這一節(jié)中你會發(fā)現(xiàn)下面的一些表達式:<br/>
<a name="fig1"></a>
<table border="0" cellpadding="0" cellspacing="0" class="regTab" align="center">
<caption>字符類</caption>
<tr>
<td class="regCenter"><code>[abc]</code></td>
<td>a, b 或 c(簡單類)</td>
</tr>
<tr>
<td class="regCenter"><code>[^abc]</code></td>
<td>除 a, b 或 c 之外的任意字符(取反)</td>
</tr>
<tr>
<td class="regCenter"><code>[a-zA-Z]</code></td>
<td>a 到 z,或 A 到 Z,包括(范圍)</td>
</tr>
<tr>
<td class="regCenter"><code>[a-d[m-p]]</code></td>
<td>a 到 d,或 m 到 p:<code>[a-dm-p]</code>(并集)</td>
</tr>
<tr>
<td class="regCenter"><code>[a-z&&[def]]</code></td>
<td>d,e 或 f(交集)</td>
</tr>
<tr>
<td class="regCenter"><code>[a-z&&[^bc]]</code></td>
<td>除 b 和 c 之外的 a 到 z 字符:<code>[ad-z]</code>(差集)</td>
</tr>
<tr>
<td class="regCenter"><code>[a-z&&[^m-p]]</code></td>
<td>a 到 z,并且不包括 m 到 p:<code>[a-lq-z]</code>(差集)</td>
</tr>
</table>
左邊列指定正則表達式構(gòu)造,右邊列描述每個構(gòu)造的匹配的條件。<br/>
<p id="tip">
注意:“字符類(character class)”這個詞中的“類(class)”指的并不是一個 .class 文件。在正則表達式的語義中,字符類是放在方括號里的字符集,指定了一些字符中的一個能被給定的字符串所匹配。
</p>
<div id="h3"><a name="reg3_1"></a>3.1 簡單類(Simple Classes)<span class="returnContents"><a href="#contents">返回目錄</a></span></div>
字符類最基本的格式是把一些字符放在一對方括號內(nèi)。例如:正則表達式<code>[bcr]at</code>會匹配“bat”、“cat”或者“rat”,這是由于其定義了一個字符類(接受“b”、“c”或“r”中的一個字符)作為它的首字符。
<pre id="console">Enter your regex: [bcr]at
Enter input string to search: bat
I found the text "bat" starting at index 0 and ending at index 3.
Enter your regex: [bcr]at
Enter input string to search: cat
I found the text "cat" starting at index 0 and ending at index 3.
Enter your regex: [bcr]at
Enter input string to search: rat
I found the text "rat" starting at index 0 and ending at index 3.
Enter your regex: [bcr]at
Enter input string to search: hat
No match found.</pre>
在上面的例子中,在第一個字符匹配字符類中所定義字符中的一個時,整個匹配就是成功的。
<div id="h4"><a name="reg3_1_1"></a>3.1.1 否定<span class="returnContents"><a href="#contents">返回目錄</a></span></div>
要匹配除那些列表之外所有的字符時,可以在字符類的開始處加上<code>^</code>元字符,這種就被稱為<em>否定</em>(negation)。
<pre id="console">Enter your regex: [^bcr]at
Enter input string to search: bat
No match found.
Enter your regex: [^bcr]at
Enter input string to search: cat
No match found.
Enter your regex: [^bcr]at
Enter input string to search: rat
No match found.
Enter your regex: [^bcr]at
Enter input string to search: hat
I found the text "hat" starting at index 0 and ending at index 3.</pre>
在輸入的字符串中的第一個字符不包含在字符類中所定義字符中的一個時,匹配是成功的。
<div id="h4"><a name="reg3_1_2"></a>3.1.2 范圍<span class="returnContents"><a href="#contents">返回目錄</a></span></div>
有時會想要定義一個包含值范圍的字符類,諸如,“a 到 h”的字母或者是“1 到 5”的數(shù)字。指定一個范圍,只要在被匹配的首字符和末字符間插入<code>-</code>元字符,比如:<code>[1-5]</code>或者是<code>[a-h]</code>。也可以在類里每個的邊上放置不同的范圍來提高匹配的可能性,例如:<code>[a-zA-Z]</code>將會匹配 a 到 z(小寫字母)或者 A 到 Z(大寫字母)中的任何一個字符。<br/>
下面是一些范圍和否定的例子:<br/>
<pre id="console">Enter your regex: [a-c]
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
Enter your regex: [a-c]
Enter input string to search: b
I found the text "b" starting at index 0 and ending at index 1.
Enter your regex: [a-c]
Enter input string to search: c
I found the text "c" starting at index 0 and ending at index 1.
Enter your regex: [a-c]
Enter input string to search: d
No match found.
Enter your regex: foo[1-5]
Enter input string to search: foo1
I found the text "foo1" starting at index 0 and ending at index 4.
Enter your regex: foo[1-5]
Enter input string to search: foo5
I found the text "foo5" starting at index 0 and ending at index 4.
Enter your regex: foo[1-5]
Enter input string to search: foo6
No match found.
Enter your regex: foo[^1-5]
Enter input string to search: foo1
No match found.
Enter your regex: foo[^1-5]
Enter input string to search: foo6
I found the text "foo6" starting at index 0 and ending at index 4.</pre>
<div id="h4"><a name="reg3_1_3"></a>3.1.3 并集<span class="returnContents"><a href="#contents">返回目錄</a></span></div>
可以使用<em>并集</em>(union)來建一個由兩個或兩個以上字符類所組成的單字符類。構(gòu)建一個并集,只要在一個字符類的邊上嵌套另外一個,比如:<code>[0-4[6-8]]</code>,這種奇特方式構(gòu)建的并集字符類,可以匹配 0,1,2,3,4,6,7,8 這幾個數(shù)字。
<pre id="console">Enter your regex: [0-4[6-8]]
Enter input string to search: 0
I found the text "0" starting at index 0 and ending at index 1.
Enter your regex: [0-4[6-8]]
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -