2

我需要一个可以与replaceAllString 类的方法一起使用的正则表达式来替换所有实例,*除了.*一个带有尾随的实例\

即转换将是

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

有人可以帮帮我吗?

4

2 回答 2

4

使用后视。

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
于 2011-11-17T08:58:02.600 回答
1

可能没有捕获组,但这应该有效:

myString.replaceAll("\\*([^\\\\]|$)", "*.$1");
于 2011-11-17T08:56:56.367 回答