Java正則表達(dá)式API Matcher類(lèi)方法
一、Pattern.DOTALL
默認(rèn)情況下,當(dāng)我們使用“.”時(shí)表達(dá)式中,我們將匹配輸入字符串中的每個(gè)字符,直到遇到新行字符。
使用此標(biāo)志,匹配也將包括行終止符。我們將通過(guò)以下示例更好地理解。這些例子將略有不同。由于我們感興趣的是針對(duì)匹配的字符串進(jìn)行斷言,因此我們將使用matcher的group方法來(lái)返回之前的匹配。
首先,我們將看到默認(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á)式來(lái)啟用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對(duì)任何元字符、轉(zhuǎn)義字符或正則表達(dá)式語(yǔ)法都沒(méi)有特殊意義。如果沒(méi)有此標(biāo)志,匹配器將根據(jù)任何輸入字符串匹配以下正則表達(dá)式:
@Test
public void givenRegex_whenMatchesWithoutLiteralFlag_thenCorrect() {
int matches = runTest("(.*)", "text");
assertTrue(matches > 0);
}
這是我們?cè)谒惺纠锌吹降哪J(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)在,如果我們添加所需的字符串,測(cè)試將通過(guò):
@Test
public void givenRegex_whenMatchesWithLiteralFlag_thenCorrect() {
int matches = runTest("(.*)", "text(.*)", Pattern.LITERAL);
assertTrue(matches > 0);
}沒(méi)有用于啟用文字分析的嵌入標(biāo)志字符。
三、Pattern.MULTILINE
默認(rèn)情況下,^和$元字符分別在整個(gè)輸入字符串的開(kāi)頭和結(jié)尾絕對(duì)匹配。匹配器忽略任何行終止符:
@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)志,同樣的測(cè)試也會(huì)通過(guò),因?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類(lèi)方法
在本節(jié)中,我們將研究Matcher類(lèi)的一些有用方法。為了清晰起見(jiàn),我們將根據(jù)功能對(duì)它們進(jìn)行分組。
索引方法
索引方法提供有用的索引值,精確顯示在輸入字符串中找到匹配項(xiàng)的位置。在下面的測(cè)試中,我們將確認(rèn)輸入字符串中dog匹配的開(kāi)始和結(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則不需要。
這兩種方法都從輸入字符串的開(kāi)頭開(kāi)始:
@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方法可用于替換輸入字符串中的文本。常見(jiàn)的是replaceFirst和replaceAll。
replaceFirst和replaceAll方法替換與給定正則表達(dá)式匹配的文本。正如其名稱(chēng)所示,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類(lèi)方法的文章就介紹到這了,更多相關(guān)Java正則表達(dá)式 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java設(shè)計(jì)模式之單例模式的詳解及優(yōu)點(diǎn)
這篇文章主要介紹了java設(shè)計(jì)模式之單例模式的詳解及優(yōu)點(diǎn)的相關(guān)資料,如果一個(gè)類(lèi)始終只能創(chuàng)建一個(gè)實(shí)例,那么這個(gè)類(lèi)被稱(chēng)為單例類(lèi),這種設(shè)計(jì)模式被稱(chēng)為單例模式,需要的朋友可以參考下2017-08-08
Springboot整合SpringSecurity的完整案例詳解
Spring Security是基于Spring生態(tài)圈的,用于提供安全訪問(wèn)控制解決方案的框架,Spring Security登錄認(rèn)證主要涉及兩個(gè)重要的接口 UserDetailService和UserDetails接口,本文對(duì)Springboot整合SpringSecurity過(guò)程給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01
Java使用NioSocket手動(dòng)實(shí)現(xiàn)HTTP服務(wù)器
本篇文章主要介紹了Java使用NioSocket手動(dòng)實(shí)現(xiàn)HTTP服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-05-05
Spring Boot Admin Server管理客戶(hù)端過(guò)程詳解
這篇文章主要介紹了Spring Boot Admin Server管理客戶(hù)端過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
SpringBoot去除內(nèi)嵌tomcat的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot去除內(nèi)嵌tomcat的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
java實(shí)現(xiàn)計(jì)算周期性提醒的示例
本文分享一個(gè)java實(shí)現(xiàn)計(jì)算周期性提醒的示例,可以計(jì)算父親節(jié)、母親節(jié)這樣的節(jié)日,也可以定義如每月最好一個(gè)周五,以方便安排會(huì)議2014-04-04

