簡體   English   中英

如何在Java中使用正則表達式匹配函數匹配具有元字符的字符串

[英]How to match a String having metacharacters using regex matches function in Java

字符串下方要匹配的正則表達式是什么

String str = "<Element>\r\n <Sub>regular</Sub></Element>";

有一個

carriage return "\r", new line character "\n" and a space after <Element>.

我的代碼如下

if(str.matches("<Element>([\\s])<Sub>(.*)"))
{
     System.out.println("Matches");
}

使用“點匹配換行符”開關:

if (str.matches("(?s)<Element>\\s*<Sub>(.*)"))

打開開關后, \\s將匹配換行符。

我也修復了您的正則表達式,刪除了兩組多余的括號,並在空格正則表達式之后添加了關鍵*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM