簡體   English   中英

String.replaceAll的正則表達式

[英]Regular expression for String.replaceAll

我需要一個可與String類的replaceAll方法一起使用的正則表達式,以將*所有實例替換為.*但帶有尾隨\\

即轉換將是

[any character]*[any character] => [any character].*[any character]
* => .*
\* => \* (i.e. no conversion.)

有人可以幫幫我嗎?

使用后向。

String resultString = subjectString.replaceAll("(?<!\\\\)\\*", ".*");

說明:

"(?<!" +     // Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   "\\\\" +       // Match the character “\” literally
")" +
"\\*"         // Match the character “*” literally

可能沒有捕獲組也可以這樣做,但這應該可行:

myString.replaceAll("\\*([^\\\\]|$)", "*.$1");

暫無
暫無

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

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