1

Input is something like this:

String text = "{\"definitions\":[{\"id\":\"com.sample.evaluationa\",\"name\":\"Evaluationa\",\"vers}{\"id\":\"com.sample.evaluationb\",\"name\":\"Evaluationb\",\"vers}";

And some quotes to make it more transparent

String definitions = "{\"definitions\":[";
String id = "{\"id\":\"";
String name = "\",\"name\":\"";
String rest = "\",\"vers}";

My regular expression then looks like this:

Pattern pattern = Pattern.compile((Pattern.quote(definitions)) +"("+ (Pattern.quote(id)) +"(.+)" +(Pattern.quote(name))+"(.+)"+(Pattern.quote(rest))+")*");

I am looking for id's (com.sample.evaluation)

Matcher regexMatcher = pattern.matcher(text);
    while (regexMatcher.find()) {
        title = regexMatcher.group(2);
        System.out.println(title);
        System.out.println("The pattern is " + pattern.pattern());
    }

My output looks like this:

com.sample.evaluationa","name":"Evaluationa","vers}{"id":"com.sample.evaluationb
The pattern is \Q{"definitions":[\E(\Q{"id":"\E(.+)\Q","name":"\E(.+)\Q","vers}\E)*

But I want:

com.sample.evaluationacom.sample.evluationb

And what is also interesting but not in the good way, after changing targeted group in cycle

title = regexMatcher.group(2);

I get just (and of course the pattern line)

Evaluationb
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
toothbrush
  • 43
  • 1
  • 5

2 Answers2

1

.+ means 1 to infinite characters and the regex engine is by default greedy so it will match as many characters as possible.

It would be better to define classes of characters that you can expect there:
[a-z\.]+ for id and [A-Z][a-z]+ for name
those will work by themselves because when hitting the \ the regex engine will stop as \ is not part of the last class

If you by any reason want to use .+ but stop as early as possible add a ? after so it will switch to lazy behaviour. (eg. .+?)

CSᵠ
  • 10,049
  • 9
  • 41
  • 64
0

You can handle that string in your java, just download Apache commons lang library and paste the commons-lang3-3.1.jar file to your libs folder

And use it like this

String formatedjsonstring=StringEscapeUtils.unescapeJava(yourjsonstring);

Don't make the things complicated..Use this library which will automatically unescape the characters

After you java escape the characters create Json Object like this

JSONObject obj=new JSONObject(formatedjsonstring);
JSONArray jsonArray=obj.getJSONArray("definitions");

Than use for loop to parse those json objects

for(int i=0;i<jsonArray.length();i++)
{
JSONObject obj=jsonArray.getJSONObject(i);
String id=obj.getString("id");
}
Pragnani
  • 20,075
  • 6
  • 49
  • 74