0

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?

5
  • Can you share what you have done in java ? Commented Dec 12, 2019 at 6:10
  • {.*} is giving "Unable to execute regular expression. java.util.regex.PatternSyntaxException: Illegal repetition {.*}" error Commented Dec 12, 2019 at 6:10
  • i have updated the question with it @soorapadman Commented Dec 12, 2019 at 6:12
  • Can you show your Java code in question. Commented Dec 12, 2019 at 6:16
  • I intend to use this regex in wso2sp , as this platform is java based, regex needs to be in JAVA format , i am testing the above regex on freeformatter.com/java-regex-tester.html Commented Dec 12, 2019 at 6:19

2 Answers 2

2

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"}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.