[英]Regular expression for String.replaceAll
I need a regular expression that can be used with replaceAll
method of String class to replace all instance of *
with .*
except for one that has trailing \\
我需要一个可与String类的
replaceAll
方法一起使用的正则表达式,以将*
所有实例替换为.*
但带有尾随\\
ie conversion would be 即转换将是
[any character]*[any character] => [any character].*[any character]
* => .*
\* => \* (i.e. no conversion.)
Can someone please help me? 有人可以帮帮我吗?
Use lookbehind. 使用后向。
String resultString = subjectString.replaceAll("(?<!\\\\)\\*", ".*");
Explanation : 说明:
"(?<!" + // 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.