Noomak
Noomak

Reputation: 371

Java Regex to get JSON schema

I need to get a JSON object from a String

in this string the JSON content is unique with this form:

abit2:{....};

I made my regex this way, but I'm unable to stop at semicolon

public String getJSON(String content) throws MalformedURLException, IOException{

    String json = "";       

    String regex = "abit2:([\\s\\S]*)\\};"; 
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(content);     
    while (matcher.find()) {            
        json = matcher.group().replace("abit2:", "");       
    }

    return json;        
}

Upvotes: 1

Views: 255

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

You forget to include the opening curly brace and you need to make this [\\s\\S]* pattern as non-greedy.

String regex = "abit2:\\{([\\s\\S]*?)\\};";

DEMO

Upvotes: 1

Related Questions