0

I would like to use regex to extract the values used inside the second []

[
"Shoes for men wither",
"Shoes for men wither leather",
]

enter image description here

I have tried the following: check all the words between quotes.

 Matcher m = Pattern.compile("\"([^\"]*)\"").matcher(response);

The problem with the above is that it gives me the other values outside the inner array.

6
  • I think it's much easier without regex. You need 1st open bracket and then first closing bracket right after. Commented Nov 21, 2019 at 23:25
  • You mean just with String.replace() and without using regex? Commented Nov 21, 2019 at 23:27
  • What I said will give you start and end points for substr() . Commented Nov 21, 2019 at 23:31
  • Correction. 2nd opening bracket. Commented Nov 21, 2019 at 23:33
  • The problem is I don't know the index of the second [ ] as the json output string can vary. it could be [ "abc" [ "def", "ghi"] ], here I want the values "def", "ghi" Commented Nov 21, 2019 at 23:34

1 Answer 1

1

This looks like JSON, which is not a regular language, and thus can't be parsed reliably with regex. You would almost certainly be better off using a JSON parsing library, such as Gson or one of the many listed at json.org.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree you are right. How would you put in a Wrapper.java class for this JSON output completion.amazon.com/search/…
If you were using Gson, you'd probably want to decode it with JsonParser.parseString and call getAsJsonArray() on it, and grab the second element of that as a JsonArray as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.