I have the following input string:
flag1 == 'hello' and flag2=='hello2'
(the string length and == 'something' varies).
Desired output:
flag1==("hello") and flag2=("hello2")
I have tried
line = line.replaceAll("(\\s*==\\s*)", "(\"")
but that does not give me the end bracket. Any idea how this can be done?
Thanks!
Unless I'm misunderstanding, you could match everything between the quotes and replace.
String s = "flag1 == 'hello' and flag2=='hello2'";
s = s.replaceAll("'([^']+)'", "(\"$1\")");
System.out.println(s); // flag1 == ("hello") and flag2==("hello2")
If you want the whitespace around ==
replaced:
s = s.replaceAll("\\s*==\\s*'([^']+)'", "==(\"$1\")");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With