Hi I've been trying to create a Regex in Java to match JSON data that I have converted to a string using json_encode. I've been reading through examples on stackoverflow but I'm not sure do they just relate to pure JSON or strings containing JSON representation. I've tried applying a few but can't get them to work.
Looking here:
And here:
This is the string I am trying to use my Regex to match to:
[{"resourceUri":"file:\/home\/admin\/test-modeling\/apache-tomcat-7.0.70\/temp\/IOS\/filetest-file-files.txt#\/\/@statements.12\/@typeList.0\/@enumLiterals.11","severity":"WARNING","lineNumber":333,"column":9,"offset":7780,"length":24,"message":"Enum name should be less than 20 characters"}]
I've tried using this answer and it matches fine when I use regex101 for testing.
https://stackoverflow.com/a/6249375/5476612
I'm using this regex from here:
/\A("([^"\\]*|\\["\\bfnrt\/]|\\u[0-9a-f]{4})*"|-?(?=[1-9]|0(?!\d))\d+(\.\d+)?([eE][+-]?\d+)?|true|false|null|\[(?:(?1)(?:,(?1))*)?\s*\]|\{(?:\s*"([^"\\]*|\\["\\bfnrt\/]|\\u[0-9a-f]{4})*"\s*:(?1)(?:,\s*"([^"\\]*|\\["\\bfnrt\/]|\\u[0-9a-f]{4})*"\s*:(?1))*)?\s*\})\Z/is
However when I try and use it as a string in Java I get escaped character issues.
Can anyone help me with fixing the regex to work as a String to use in Java or help me create one that will work?
EDIT 1: Here's the full String I am looking at that I am trying to match the JSON string above against:
../../tool/model/toolingValidationReport.php?fileName=test-testing-types.txt&fileSize=18380&validationReport=[{"resourceUri":"file:\/home\/admin\/test-modeling\/apache-tomcat-7.0.70\/temp\/IOS\/filetest-file-files.txt#\/\/@statements.12\/@typeList.0\/@enumLiterals.11","severity":"WARNING","lineNumber":333,"column":9,"offset":7780,"length":24,"message":"Enum name should be less than 20 characters"}] target=
EDIT 2: Here's the Java I am using to perform the regex check. The href
variable contains the String content shown in edit 1.
Pattern validationReportPattern = Pattern.compile(getValidationReportPattern());
Matcher validationReportMatcher = validationReportPattern.matcher(href);
public String getYangValidationReportPattern(){
return "(\\[\\{.*\\}])";
}
String validationReport = validationReportMatcher.group(1);