Java正則表達式API Matcher類方法
一、Pattern.DOTALL
默認情況下,當我們使用“.”時表達式中,我們將匹配輸入字符串中的每個字符,直到遇到新行字符。
使用此標志,匹配也將包括行終止符。我們將通過以下示例更好地理解。這些例子將略有不同。由于我們感興趣的是針對匹配的字符串進行斷言,因此我們將使用matcher的group方法來返回之前的匹配。
首先,我們將看到默認行為:
@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)); }
正如我們所看到的,只有行終止符之前輸入的第一部分匹配。
現在在dotall模式下,包括行終止符在內的整個文本都將匹配:
@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)); }
我們還可以使用嵌入的標志表達式來啟用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對任何元字符、轉義字符或正則表達式語法都沒有特殊意義。如果沒有此標志,匹配器將根據任何輸入字符串匹配以下正則表達式:
@Test public void givenRegex_whenMatchesWithoutLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text"); assertTrue(matches > 0); }
這是我們在所有示例中看到的默認行為。但是,使用此標志時,將找不到匹配項,因為匹配器將查找(.*)
,而不是解釋它:
@Test public void givenRegex_whenMatchFailsWithLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text", Pattern.LITERAL); assertFalse(matches > 0); }
現在,如果我們添加所需的字符串,測試將通過:
@Test public void givenRegex_whenMatchesWithLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text(.*)", Pattern.LITERAL); assertTrue(matches > 0); }
沒有用于啟用文字分析的嵌入標志字符。
三、Pattern.MULTILINE
默認情況下,^
和$
元字符分別在整個輸入字符串的開頭和結尾絕對匹配。匹配器忽略任何行終止符:
@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); }
匹配失敗,因為匹配器在整個字符串的末尾搜索dog
,但狗出現在字符串第一行的末尾。
然而,有了這個標志,同樣的測試也會通過,因為匹配器現在考慮了行終止符。因此,字符串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); }
以下是嵌入式標志版本:
@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
類的一些有用方法。為了清晰起見,我們將根據功能對它們進行分組。
索引方法
索引方法提供有用的索引值,精確顯示在輸入字符串中找到匹配項的位置。在下面的測試中,我們將確認輸入字符串中dog
匹配的開始和結束索引:
@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方法遍歷輸入字符串并返回一個布爾值,指示是否找到該模式。常用的是matches
和lookingAt
方法。
matches
和lookingAt
方法都試圖根據模式匹配輸入序列。不同之處在于,匹配需要匹配整個輸入序列,而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
方法替換與給定正則表達式匹配的文本。正如其名稱所示,replaceFirst
替換第一個引用,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
方法允許我們用相同的替換替換所有匹配項。
到此這篇關于Java正則表達式API Matcher類方法的文章就介紹到這了,更多相關Java正則表達式 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
簡單談談java的異常處理(Try Catch Finally)
在程序設計中,進行異常處理是非常關鍵和重要的一部分。一個程序的異常處理框架的好壞直接影響到整個項目的代碼質量以及后期維護成本和難度。2016-03-03SpringBoot整合Security安全框架實現控制權限
本文主要介紹了SpringBoot整合Security安全框架實現控制權限,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Java Socket聊天室編程(一)之利用socket實現聊天之消息推送
這篇文章主要介紹了Java Socket聊天室編程(一)之利用socket實現聊天之消息推送的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09java 中InputStream,String,File之間的相互轉化對比
這篇文章主要介紹了java 中InputStream,String,File之間的相互轉化對比的相關資料,需要的朋友可以參考下2017-04-04