如果我有字符串 "This is > than" something 1=3 and "else = true"
(不要试图理解它的含义,它只是一个示例:P)我如何替换引号中未包含的自定义模式?例如,将第一个运算符“=”(等于)替换为“<”,结果为字符串 "This is > than" something 1<3 and "else = true"
。预先感谢您。
最佳答案
您可以尝试以下正则表达式来替换所有 =
它存在于 "
之外与 <
符号。
=(?=(?:[^"]*"[^"]*")*[^"]*$)
替换字符串:
<
System.out.println("\"This is > than\" something 1=3 and \"else = true\"".replaceAll("=(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", "<"));
输出:
"This is > than" something 1<3 and "else = true"
关于java - 正则表达式: ignore what is quoted in a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644144/