I have Strings like this:
fields {
key: "ristorante"
value {
string_value: ""
}
}
fields {
key: "type"
value {
list_value {
values {
string_value: "pizza"
}
}
}
}
And I need to extract just the string_value of type, in this case the word pizza .
How can I do it in Java?
I don't want the answer for this specific case, I just want to know how to extract some text from a String or if is better to parse it to a JSON, and how to do it.
Pattern pattern = Pattern.compile("values[^s]+?string_value\\s?:\\s?\"(.*?)(?<!\\\\)\"");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
String value = matcher.group(1);
System.out.println(value);
}
Use this regex if you really only want to get the 'pizza' value
If you want to get all 'string_value' entries use
"string_value\\s?:\\s?\"(.*?)(?<!\\\\)\""
as the regex pattern. You will then find multiple occurrences, so use a while loop instead of if (matcher.find)
(Regex will probably be a little bit faster than parsing it with JsonParser)
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.