我需要一个可以与replaceAll
String 类的方法一起使用的正则表达式来替换所有实例,*
除了.*
一个带有尾随的实例\
即转换将是
[any character]*[any character] => [any character].*[any character]
* => .*
\* => \* (i.e. no conversion.)
有人可以帮帮我吗?
我需要一个可以与replaceAll
String 类的方法一起使用的正则表达式来替换所有实例,*
除了.*
一个带有尾随的实例\
即转换将是
[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");