I am getting the following string in my java app from a client.
[["UIButton","Submit","15","30","80","80"],["UILabel","User name","15","75","80","80"],["UITextField","Jonathan","15","75","80","80"]]ˇ˛END
I am expecting to parse this and take out separately as 3 strings as like below:
"UIButton","Submit","15","30","80","80"
"UIButton","Submit","15","30","80","80"
"UIButton","Submit","15","30","80","80"
I have tried like below,
// resultStr = [["UIButton","Submit","15","30","80","80"],["UILabel","User name","15","75","80","80"],["UITextField","Jonathan","15","75","80","80"]]ˇ˛END
if ( resultStr.length()>0 && resultStr!=null ) {
for (int i=0; i<resultStr.length(); i++) {
int startInd = resultStr.indexOf('[');
int endInd = resultStr.indexOf(']');
if ( startInd>=0 && endInd>0 ) {
String resStr = resultStr.substring(startInd , endInd);
if ( resStr!=null )
System.out.println("Applet resStr: " + resStr);
}
else
System.out.println("Applet startindex, endindex failed");
}
}
But, this is not working, this is not rightly parsing like what I expected. Could someone advise how to parse the string separately as I expected ?
You can split this string using String.split()
method:
String[] t = myString.substring(2, s.length()-7).split("\\],\\[");
So your code will look like this:
//resultStr = [["UIButton","Submit","15","30","80","80"],["UILabel","User name","15","75","80","80"],["UITextField","Jonathan","15","75","80","80"]]ˇ˛END
if ( resultStr.length()>7 && resultStr!=null ) {
String[] resStrings = resultStr.substring(2, resultStr.length()-7).split("\\],\\[");
for (String resString: resStrings) {
System.out.println("Applet resStr: " + resStr);
}
}
As a result, you should have following output:
Applet resStr: "UIButton","Submit","15","30","80","80"
Applet resStr: "UILabel","User name","15","75","80","80"
Applet resStr: "UITextField","Jonathan","15","75","80","80"
Try with Pattern and Matcher using Lazy way and Lookaround
(?<=\[).*?(?=\])
OR possessive quantifier
(?<=\[)[^]]*+(?=\])
Sample code:
Matcher matcher = Pattern.compile("(?<=\\[).*?(?=\\])").matcher(strring);
while (matcher.find()) {
System.out.println(matcher.group());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.