Java正則表達(dá)式API Matcher類方法
一、Pattern.DOTALL
默認(rèn)情況下,當(dāng)我們使用“.”時(shí)表達(dá)式中,我們將匹配輸入字符串中的每個(gè)字符,直到遇到新行字符。
使用此標(biāo)志,匹配也將包括行終止符。我們將通過以下示例更好地理解。這些例子將略有不同。由于我們感興趣的是針對匹配的字符串進(jìn)行斷言,因此我們將使用matcher的group方法來返回之前的匹配。
首先,我們將看到默認(rèn)行為:
@Test public void givenRegexWithLineTerminator_whenMatchFails_thenCorrect() { Pattern pattern = Pattern.compile("(.*)"); Matcher matcher = pattern.matcher( "this is a text" + System.getProperty("line.separator") + " continued on another line"); matcher.find(); assertEquals("this is a text", matcher.group(1)); }
正如我們所看到的,只有行終止符之前輸入的第一部分匹配。
現(xiàn)在在dotall模式下,包括行終止符在內(nèi)的整個(gè)文本都將匹配:
@Test public void givenRegexWithLineTerminator_whenMatchesWithDotall_thenCorrect() { Pattern pattern = Pattern.compile("(.*)", Pattern.DOTALL); Matcher matcher = pattern.matcher( "this is a text" + System.getProperty("line.separator") + " continued on another line"); matcher.find(); assertEquals( "this is a text" + System.getProperty("line.separator") + " continued on another line", matcher.group(1)); }
我們還可以使用嵌入的標(biāo)志表達(dá)式來啟用dotall模式:
@Test public void givenRegexWithLineTerminator_whenMatchesWithEmbeddedDotall _thenCorrect() { Pattern pattern = Pattern.compile("(?s)(.*)"); Matcher matcher = pattern.matcher( "this is a text" + System.getProperty("line.separator") + " continued on another line"); matcher.find(); assertEquals( "this is a text" + System.getProperty("line.separator") + " continued on another line", matcher.group(1)); }
二、Pattern.LITERAL
在此模式下,matcher對任何元字符、轉(zhuǎn)義字符或正則表達(dá)式語法都沒有特殊意義。如果沒有此標(biāo)志,匹配器將根據(jù)任何輸入字符串匹配以下正則表達(dá)式:
@Test public void givenRegex_whenMatchesWithoutLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text"); assertTrue(matches > 0); }
這是我們在所有示例中看到的默認(rèn)行為。但是,使用此標(biāo)志時(shí),將找不到匹配項(xiàng),因?yàn)槠ヅ淦鲗⒉檎?code>(.*),而不是解釋它:
@Test public void givenRegex_whenMatchFailsWithLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text", Pattern.LITERAL); assertFalse(matches > 0); }
現(xiàn)在,如果我們添加所需的字符串,測試將通過:
@Test public void givenRegex_whenMatchesWithLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text(.*)", Pattern.LITERAL); assertTrue(matches > 0); }
沒有用于啟用文字分析的嵌入標(biāo)志字符。
三、Pattern.MULTILINE
默認(rèn)情況下,^
和$
元字符分別在整個(gè)輸入字符串的開頭和結(jié)尾絕對匹配。匹配器忽略任何行終止符:
@Test public void givenRegex_whenMatchFailsWithoutMultilineFlag_thenCorrect() { int matches = runTest( "dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox"); assertFalse(matches > 0); }
匹配失敗,因?yàn)槠ヅ淦髟谡麄€(gè)字符串的末尾搜索dog
,但狗出現(xiàn)在字符串第一行的末尾。
然而,有了這個(gè)標(biāo)志,同樣的測試也會(huì)通過,因?yàn)槠ヅ淦鳜F(xiàn)在考慮了行終止符。因此,字符串dog
正好在行終止之前找到,因此成功:
@Test public void givenRegex_whenMatchesWithMultilineFlag_thenCorrect() { int matches = runTest( "dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox", Pattern.MULTILINE); assertTrue(matches > 0); }
以下是嵌入式標(biāo)志版本:
@Test public void givenRegex_whenMatchesWithEmbeddedMultilineFlag_ thenCorrect() { int matches = runTest( "(?m)dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox"); assertTrue(matches > 0); }
四、Matcher類方法
在本節(jié)中,我們將研究Matcher
類的一些有用方法。為了清晰起見,我們將根據(jù)功能對它們進(jìn)行分組。
索引方法
索引方法提供有用的索引值,精確顯示在輸入字符串中找到匹配項(xiàng)的位置。在下面的測試中,我們將確認(rèn)輸入字符串中dog
匹配的開始和結(jié)束索引:
@Test public void givenMatch_whenGetsIndices_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("This dog is mine"); matcher.find(); assertEquals(5, matcher.start()); assertEquals(8, matcher.end()); }
Study方法
Study方法遍歷輸入字符串并返回一個(gè)布爾值,指示是否找到該模式。常用的是matches
和lookingAt
方法。
matches
和lookingAt
方法都試圖根據(jù)模式匹配輸入序列。不同之處在于,匹配需要匹配整個(gè)輸入序列,而lookingAt
則不需要。
這兩種方法都從輸入字符串的開頭開始:
@Test public void whenStudyMethodsWork_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("dogs are friendly"); assertTrue(matcher.lookingAt()); assertFalse(matcher.matches()); }
matches
方法將在如下情況下返回true
:
@Test public void whenMatchesStudyMethodWorks_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("dog"); assertTrue(matcher.matches()); }
Replacement方法
Replacement方法可用于替換輸入字符串中的文本。常見的是replaceFirst
和replaceAll
。
replaceFirst
和replaceAll
方法替換與給定正則表達(dá)式匹配的文本。正如其名稱所示,replaceFirst
替換第一個(gè)引用,replaceAll
替換所有引用:
@Test public void whenReplaceFirstWorks_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher( "dogs are domestic animals, dogs are friendly"); String newStr = matcher.replaceFirst("cat"); assertEquals( "cats are domestic animals, dogs are friendly", newStr); }
替換所有引用:
@Test public void whenReplaceAllWorks_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher( "dogs are domestic animals, dogs are friendly"); String newStr = matcher.replaceAll("cat"); assertEquals("cats are domestic animals, cats are friendly", newStr); }
replaceAll
方法允許我們用相同的替換替換所有匹配項(xiàng)。
到此這篇關(guān)于Java正則表達(dá)式API Matcher類方法的文章就介紹到這了,更多相關(guān)Java正則表達(dá)式 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡單談?wù)刯ava的異常處理(Try Catch Finally)
在程序設(shè)計(jì)中,進(jìn)行異常處理是非常關(guān)鍵和重要的一部分。一個(gè)程序的異常處理框架的好壞直接影響到整個(gè)項(xiàng)目的代碼質(zhì)量以及后期維護(hù)成本和難度。2016-03-03Java實(shí)現(xiàn)的時(shí)間戳與date對象相互轉(zhuǎn)換功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的時(shí)間戳與date對象相互轉(zhuǎn)換功能,結(jié)合具體實(shí)例形式分析了java日期與時(shí)間戳類型的表示與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-06-06SpringBoot整合Security安全框架實(shí)現(xiàn)控制權(quán)限
本文主要介紹了SpringBoot整合Security安全框架實(shí)現(xiàn)控制權(quán)限,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Spring Native 基礎(chǔ)環(huán)境搭建過程
Spring?Native可以通過GraalVM將Spring應(yīng)用程序編譯成原生鏡像,提供了一種新的方式來部署Spring應(yīng)用,本文介紹Spring?Native基礎(chǔ)環(huán)境搭建,感興趣的朋友跟隨小編一起看看吧2024-02-02Java SHA-256加密的兩種實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java SHA-256加密的兩種實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)SHA-256加密的實(shí)現(xiàn)代碼與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08Java Socket聊天室編程(一)之利用socket實(shí)現(xiàn)聊天之消息推送
這篇文章主要介紹了Java Socket聊天室編程(一)之利用socket實(shí)現(xiàn)聊天之消息推送的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09java 中InputStream,String,File之間的相互轉(zhuǎn)化對比
這篇文章主要介紹了java 中InputStream,String,File之間的相互轉(zhuǎn)化對比的相關(guān)資料,需要的朋友可以參考下2017-04-04