input text:
inptext = "inp1(A, Var1), inp1(B,Var1)"
expected output:
optext = "inp1(A, Var1)", "inp1(B,Var1)"
Code:
String [] splitText = inptext.split(", ");
for (String obj:splitText )
{
System.out.println(obj);
}
Current output:
inp1(A
Var1)
.
.
Interpreting the current output:
optext = "inp1(A", "Var1)"
Please suggest me on correcting this.
You can use Regex lookahead to achieve this, e.g.:
String inptext = "inp1(A, Var1), inp1(B,Var1)";
String[] tokens = inptext.split("(?<=\\)),\\s");
for(String token : tokens){
System.out.println(token);
}
if you have space after the comma in the input "like inp1" you can use this:
String inptext = "inp1(A, Var1), inp1(B,Var1)";
String[] tmp = inptext.split("\\), ");
for (String a : tmp) {
if (!a.substring(a.length()-1).equals(")"))
a += ")";
System.out.println(a);
}
you could combine String#substring and String#indexOf to retrieve the required data.
String inptext = "inp1(A, Var1), inp1(B,Var1)";
String firstResult = inptext.substring(0,inptext.lastIndexOf(", "));
String secondResult = inptext.substring(inptext.lastIndexOf(", ")).replace(", ","");