Boundary Matchers
Java regex API還支持邊界匹配翔烁。如果我們關(guān)心在輸入文本中匹配的確切位置删铃,那么這就是我們要尋找的权薯。在前面的示例中鸵赖,我們關(guān)心的只是是否找到匹配項(xiàng)。
為了僅在文本開(kāi)頭所需的正則表達(dá)式為true時(shí)匹配,我們使用插入符號(hào)^。
此測(cè)試將失敗,因?yàn)榭梢栽陂_(kāi)頭找到文本 dog :
@Test
public void givenText_whenMatchesAtBeginning_thenCorrect() {
int matches = runTest("^dog", "dogs are friendly");
assertTrue(matches > 0);
}
下面的測(cè)試將失敳诩啊:
@Test
public void givenTextAndWrongInput_whenMatchFailsAtBeginning_
thenCorrect() {
int matches = runTest("^dog", "are dogs are friendly?");
assertFalse(matches > 0);
}
為了僅在文本末尾所需的正則表達(dá)式為true時(shí)匹配,我們使用美元字符 $ 筛欢。在以下情況下會(huì)找到匹配項(xiàng):
@Test
public void givenText_whenMatchesAtEnd_thenCorrect() {
int matches = runTest("dog$", "Man's best friend is a dog");
assertTrue(matches > 0);
}
并且沒(méi)有找到匹配:
@Test
public void givenTextAndWrongInput_whenMatchFailsAtEnd_thenCorrect() {
int matches = runTest("dog$", "is a dog man's best friend?");
assertFalse(matches > 0);
}
如果僅在單詞邊界處找到所需文本時(shí)才需要匹配浸锨,則在正則表達(dá)式的開(kāi)頭和結(jié)尾使用 \b 正則表達(dá)式:
空格是單詞邊界:
@Test
public void givenText_whenMatchesAtWordBoundary_thenCorrect() {
int matches = runTest("\bdog\b", "a dog is friendly");
assertTrue(matches > 0);
}
行首的空字符串也是單詞邊界:
@Test
public void givenText_whenMatchesAtWordBoundary_thenCorrect2() {
int matches = runTest("\bdog\b", "dog is man's best friend");
assertTrue(matches > 0);
}
這些測(cè)試之所以通過(guò),是因?yàn)樽址拈_(kāi)頭以及文本之間的空格標(biāo)記了單詞邊界版姑,但是以下測(cè)試顯示了相反的結(jié)果:
@Test
public void givenWrongText_whenMatchFailsAtWordBoundary_thenCorrect() {
int matches = runTest("\bdog\b", "snoop dogg is a rapper");
assertFalse(matches > 0);
}
一行中出現(xiàn)的兩個(gè)單詞字符不會(huì)標(biāo)記單詞邊界柱搜,但我們可以通過(guò)更改正則表達(dá)式的結(jié)尾來(lái)查找非單詞邊界:
@Test
public void givenText_whenMatchesAtWordAndNonBoundary_thenCorrect() {
int matches = runTest("\bdog\B", "snoop dogg is a rapper");
assertTrue(matches > 0);
}
Pattern Class Methods
之前,我們只以基本方式創(chuàng)建了模式對(duì)象剥险。然而聪蘸,這個(gè)類(lèi)有另一個(gè)compile方法的變體,它接受一組標(biāo)志以及影響模式匹配方式的regex參數(shù)表制。
這些標(biāo)志只是抽象的整數(shù)值健爬。讓我們重載test類(lèi)中的runTest方法,以便它可以將標(biāo)志作為第三個(gè)參數(shù):
public static int runTest(String regex, String text, int flags) {
pattern = Pattern.compile(regex, flags);
matcher = pattern.matcher(text);
int matches = 0;
while (matcher.find()){
matches++;
}
return matches;
}
在本節(jié)中么介,我們將了解不同的支持標(biāo)志以及它們的使用方式娜遵。
Pattern.CANON_EQ
此標(biāo)志啟用 canonical equivalence ,當(dāng)且僅當(dāng)兩個(gè)字符的完整規(guī)范分解匹配時(shí)壤短,才會(huì)認(rèn)為這兩個(gè)字符匹配设拟。
考慮帶重音的Unicode字符 é 。它的復(fù)合代碼點(diǎn)是什么 u00E9 鸽扁。但是蒜绽,Unicode的組成字符 e 、 u0065 和 u0301 也有單獨(dú)的代碼點(diǎn)桶现。在這種情況下,合成字符 u00E9 與雙字符序列 u0065 u0301 無(wú)法區(qū)分鼎姊。
默認(rèn)情況下骡和,匹配不考慮規(guī)范等效:
@Test
public void givenRegexWithoutCanonEq_whenMatchFailsOnEquivalentUnicode_thenCorrect() {
int matches = runTest("\u00E9", "\u0065\u0301");
assertFalse(matches > 0);
}
但如果添加標(biāo)志,則測(cè)試將通過(guò):
@Test
public void givenRegexWithCanonEq_whenMatchesOnEquivalentUnicode_thenCorrect() {
int matches = runTest("\u00E9", "\u0065\u0301", Pattern.CANON_EQ);
assertTrue(matches > 0);
}
Pattern.CASE_INSENSITIVE
無(wú)論大小寫(xiě)相寇,此標(biāo)志都支持匹配慰于。默認(rèn)情況下,匹配會(huì)考慮大小寫(xiě):
@Test
public void givenRegexWithDefaultMatcher_whenMatchFailsOnDifferentCases_thenCorrect() {
int matches = runTest("dog", "This is a Dog");
assertFalse(matches > 0);
}
因此唤衫,使用此標(biāo)志婆赠,我們可以更改默認(rèn)行為:
@Test
public void givenRegexWithCaseInsensitiveMatcher
_whenMatchesOnDifferentCases_thenCorrect() {
int matches = runTest(
"dog", "This is a Dog", Pattern.CASE_INSENSITIVE);
assertTrue(matches > 0);
}
我們還可以使用等效的嵌入標(biāo)志表達(dá)式來(lái)實(shí)現(xiàn)相同的結(jié)果:
@Test
public void givenRegexWithEmbeddedCaseInsensitiveMatcher
_whenMatchesOnDifferentCases_thenCorrect() {
int matches = runTest("(?i)dog", "This is a Dog");
assertTrue(matches > 0);
}
Pattern.COMMENTS
Java API允許在正則表達(dá)式中包含使用#的注釋。這有助于記錄復(fù)雜的正則表達(dá)式佳励,而其他程序員可能無(wú)法立即看到這些正則表達(dá)式休里。
comments標(biāo)志使matcher忽略正則表達(dá)式中的任何空白或注釋?zhuān)豢紤]模式蛆挫。在默認(rèn)匹配模式下,以下測(cè)試將失斆钍颉:
@Test
public void givenRegexWithComments_whenMatchFailsWithoutFlag_thenCorrect() {
int matches = runTest(
"dog$ #check for word dog at end of text", "This is a dog");
assertFalse(matches > 0);
}
這是因?yàn)槠ヅ淦鲗⒃谳斎胛谋局胁檎艺麄€(gè)正則表達(dá)式悴侵,包括空格和 # 字符。但當(dāng)我們使用該標(biāo)志時(shí)拭嫁,它將忽略額外的空格可免,并且以 # 開(kāi)頭的每個(gè)文本都將被視為每行要忽略的注釋?zhuān)?/p>
@Test
public void givenRegexWithComments_whenMatchesWithFlag_thenCorrect() {
int matches = runTest(
"dog$ #check end of text","This is a dog", Pattern.COMMENTS);
assertTrue(matches > 0);
}
還有一個(gè)替代的嵌入式標(biāo)志的表達(dá)方式:
@Test
public void givenRegexWithComments_whenMatchesWithEmbeddedFlag_thenCorrect() {
int matches = runTest(
"(?x)dog$ #check end of text", "This is a dog");
assertTrue(matches > 0);
}