我有以下输入字符串:
- flag1 == 'hello' and flag2=='hello2'
(字符串长度和==’某事’变化).
期望的输出:
- flag1==("hello") and flag2=("hello2")
我试过了
- line = line.replaceAll("(\\s*==\\s*)","(\"")
但这并没有给我一个结束.知道如何做到这一点?
谢谢!
解决方法
除非我误解,否则你可以匹配引号之间的所有内容并替换.
- String s = "flag1 == 'hello' and flag2=='hello2'";
- s = s.replaceAll("'([^']+)'","(\"$1\")");
- System.out.println(s); // flag1 == ("hello") and flag2==("hello2")
如果你想要==替换的空格:
- s = s.replaceAll("\\s*==\\s*'([^']+)'","==(\"$1\")");