0

I have the following code:

    public static void main(String[] args) {
    String str = "{\"$and\":[{\\\"$or\\\":[{\\\"origen\\\":{\\\"$eq\\\":\\\"LEMD\\\"}},{\\\"origen\\\":{\\\"$eq\\\":\\\"LEBL\\\"}}]},{\"horasacta\":{\"$gte\":\"28/02/2015 00:00:00\"}},{\"horasacta\":{\"$lte\":\"28/02/2015 23:59:59\"}}]}";

    Pattern pattern = Pattern.compile("\\{\"(.*?)\":\\{\"\\$(.*?)\":\"[0-9]+/[0-9]+/[0-9]+ [0-9]+:[0-9]+:[0-9]+\"}}");
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        System.out.println(matcher.group(0));
    }

I want to get the substrings:

{\"departure\":{\"$gte\":\"28/02/2015 00:00:00\"}}
{\"departure\":{\"$lte\":\"28/02/2015 23:59:59\"}}

But the program give me:

{"$and":[{\"$or\":[{\"origin\":{\"$eq\":\"LEMD\"}},{\"origin\":{\"$eq\":\"LEBL\"}}]},{"departure":{"$gte":"28/02/2015 00:00:00"}}
{"departure":{"$lte":"28/02/2015 23:59:59"}}

The 2nd time the find() matches the pattern but the 1st time it doesn't do the job.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Marvin 2
  • 41
  • 4
  • 5
    *"extract ... from a json string"* Use a **JSON Parser**!!!! – Andreas Jul 23 '19 at 22:44
  • 2
    [How to parse JSON in Java](https://stackoverflow.com/q/2591098), really regex is last tool you want here. Parsers solve all kind of problems which you are not aware of. It is same story as parsing HTML/XML with regex: [Using regular expressions to parse HTML: why not?](https://stackoverflow.com/q/590747), [Can you provide some examples of why it is hard to parse XML and HTML with a regex?](https://stackoverflow.com/q/701166), and finally https://stackoverflow.com/a/1732454, – Pshemo Jul 23 '19 at 22:45
  • Hi. I tried to parse the string with the Gson parser to convert the whole string to a object but the date string is parsed as a string value in the resulting object. What I really want is to extract those strings not to parse it. – Marvin 2 Jul 24 '19 at 07:10

1 Answers1

0

It's frowned upon, yet if you have to, I'm guessing that you may be trying to write an expression looking somewhat like:

{\\"([^\\]+)\\":{\\"\$([^\\]+)\\":\\"[0-9]+\/[0-9]+\/[0-9]+\s+[0-9]+:[0-9]+:[0-9]+\\"}}

not so sure though.


The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

Test

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

final String regex = "\\{\\\\\"([^\\\\]+)\\\\\":\\{\\\\\"\\$([^\\\\]+)\\\\\":\\\\\"[0-9]+\\/[0-9]+\\/[0-9]+\\s+[0-9]+:[0-9]+:[0-9]+\\\\\"\\}}";
final String string = "{\\\"departure\\\":{\\\"$gte\\\":\\\"28/02/2015 00:00:00\\\"}}\n"
     + "{\\\"departure\\\":{\\\"$lte\\\":\\\"28/02/2015 23:59:59\\\"}}";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}
Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69