[英]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.