简体   繁体   中英

JAVA regex to get entire json from text

I have a text inside which i have a valid json i want to extract entire json using regex. I am able to do it in javascript with simple regex : {.*} but not in java. I want a java compatible regex.

Sample Text with valid json:

data:""{"_id": {"$oid": "gdgdgdr"},"code": "grdgd34","name": "name1","desc": "desc","transRefId": "5debeeed8df45b0314569caa","origAmount": 1000,"amount": 1000,"currency": "USD","redeemedCashcode": "dwdw","sender": {"id": "FESFS","name": "rose","phone": "3424234232"},"receiver": {"id": "5d04c70c65c79dedb21080e1","name": "Move money from bank to wallet","phone": "63454332422","client": "biller"},"beneficiary": {},"depositor": {},"offer": "42efsgf"}""

Regex I have tried in java is : \\\\{.*\\\\}

How will i achieve the same in JAVA?

Your regular expression works perfectly fine in Java, of course.

However, you should not match the entire string, you should search for the match inside the string, and for that you use Matcher#find (as there is no convenience method in String itself):

String data = "data:\"\"{\"_id\": {\"$oid\": \"gdgdgdr\"},\"code\": \"grdgd34\",\"name\": \"name1\",\"desc\": \"desc\",\"transRefId\": \"5debeeed8df45b0314569caa\",\"origAmount\": 1000,\"amount\": 1000,\"currency\": \"USD\",\"redeemedCashcode\": \"dwdw\",\"sender\": {\"id\": \"FESFS\",\"name\": \"rose\",\"phone\": \"3424234232\"},\"receiver\": {\"id\": \"5d04c70c65c79dedb21080e1\",\"name\": \"Move money from bank to wallet\",\"phone\": \"63454332422\",\"client\": \"biller\"},\"beneficiary\": {},\"depositor\": {},\"offer\": \"42efsgf\"}\"\"";

Pattern pattern = Pattern.compile("\\{.*\\}");
Matcher matcher = pattern.matcher(data);
matcher.find();
System.out.println(matcher.group());

Prints:

{"_id": {"$oid": "gdgdgdr"},"code": "grdgd34","name": "name1","desc": "desc","transRefId": "5debeeed8df45b0314569caa","origAmount": 1000,"amount": 1000,"currency": "USD","redeemedCashcode": "dwdw","sender": {"id": "FESFS","name": "rose","phone": "3424234232"},"receiver": {"id": "5d04c70c65c79dedb21080e1","name": "Move money from bank to wallet","phone": "63454332422","client": "biller"},"beneficiary": {},"depositor": {},"offer": "42efsgf"}

I have tested and your regexp works, here is the test and result

final String regex = "\\{.*\\}";
    final String string = "data:\"\"{\"_id\": {\"$oid\": \"gdgdgdr\"},\"code\": \"grdgd34\",\"name\": \"name1\",\"desc\": \"desc\",\"transRefId\": \"5debeeed8df45b0314569caa\",\"origAmount\": 1000,\"amount\": 1000,\"currency\": \"USD\",\"redeemedCashcode\": \"dwdw\",\"sender\": {\"id\": \"FESFS\",\"name\": \"rose\",\"phone\": \"3424234232\"},\"receiver\": {\"id\": \"5d04c70c65c79dedb21080e1\",\"name\": \"Move money from bank to wallet\",\"phone\": \"63454332422\",\"client\": \"biller\"},\"beneficiary\": {},\"depositor\": {},\"offer\": \"42efsgf\"}\"\"";

    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);
    matcher.find();
    System.out.println(matcher.group(0));

and the result is:

{"_id": {"$oid": "gdgdgdr"},"code": "grdgd34","name": "name1","desc": "desc","transRefId": "5debeeed8df45b0314569caa","origAmount": 1000,"amount": 1000,"currency": "USD","redeemedCashcode": "dwdw","sender": {"id": "FESFS","name": "rose","phone": "3424234232"},"receiver": {"id": "5d04c70c65c79dedb21080e1","name": "Move money from bank to wallet","phone": "63454332422","client": "biller"},"beneficiary": {},"depositor": {},"offer": "42efsgf"}

java has not any specific standard for regexp. \\\\{.*\\\\} -is just string escaped regular expression. In java character \\ is used to escape some special characters, so if you want to use this char into string as regular character it must be also escaped with extra \\ . String a = "\\\\" resolves to String which will have only one \\ . so "\\\\{.*\\\\}" resolves to a regular expression " \\{.*\\} " where \\ are also an escape characters for egexp processor to deal with { and } as symbols and not as special chars

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM