简体   繁体   中英

split string and get the value using regex from a complicated string

I have a string like this {"product_tags":"yin_yang,yin yang"} . what I want to do is just avoid everything else other than yin yang . There is two strings but I just want the first one.

Note that in some cases even if the second string is not available I want to get the same result. And that string might change so it is not necessary that the string will be always yin_yang sometimes it can be motorbike or anything else.

It Look like JSON String Use the JSONParser in java

JSONObject jobject=new JSONObject(STRING);
String value=jobject.getString("product_tags");

EDITED

Using REGEX

 String json="{\"product_tags\":\"yin_yang,yin yang\"}";
    json=json.replaceAll("([{}]+)", "");
    String value[]=json.split(":");
    System.out.print(value[1]);

You can use StringTokenizer to parse your string

String str ="{\"product_tags\":\"yin_yang,yin yang\"}";
StringTokenizer to = new StringTokenizer(str,":}");
while(to.hasMoreTokens()){
    String firstString = (String) to.nextElement();
    String secondString = (String) to.nextElement();
System.out.print(secondString);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM