0

I have a String in java that is actually a JSON object. Inside the string I am trying to find a pattern matching this

"uris" : ["www.google.com", "www.yahoo.com"]

I need help in creating a pattern string to find a match for this. I have no idea of automata theory and regular expressions.

Note: the above substring will always start at "uris" and end at "]" but there can be any number of spaces in between.

3
  • 1
    what's wrong with calling YourJsonObject.getJSONArray("uris"); and traverse the array using loop etc Commented Mar 10, 2017 at 17:19
  • the content should be always a url like your do, or it can be any thing? Commented Mar 10, 2017 at 17:19
  • 1
    Why not use proper JSON parser? You would avoid many potential problems like handling keys which only ends with uris, or searching within values of other keys like key="foo uris: [something]". Commented Mar 10, 2017 at 17:22

1 Answer 1

2

You may use the following regex :

"uris".*?\]

see regex demo / explanation

Java ( demo )

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class RegEx {
    public static void main(String[] args) {
        String s = "\"uris\" : [\"www.google.com\", \"www.yahoo.com\"]";
        String r = "\"uris\".*?\\]";
        Pattern p = Pattern.compile(r);
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group()); //"uris" : ["www.google.com", "www.yahoo.com"]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.