简体   繁体   English

正则表达式以匹配JSON编码的字符串

[英]Regex to match a JSON encoded String

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. 嗨,我一直在尝试用Java创建一个Regex来匹配我已经使用json_encode转换为字符串的JSON数据。 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. 我一直在阅读有关stackoverflow的示例,但不确定它们是否仅与纯JSON或包含JSON表示形式的字符串有关。 I've tried applying a few but can't get them to work. 我试过申请一些,但不能让他们工作。

Looking here: 在这里看:

Regex to validate JSON 正则表达式以验证JSON

And here: 和这里:

Regex to match a JSON String 正则表达式以匹配JSON字符串

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. 我尝试使用此答案,当我使用regex101进行测试时,它匹配得很好。

https://stackoverflow.com/a/6249375/5476612 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. 但是,当我尝试将其用作Java中的字符串时,遇到了转义字符问题。

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? 任何人都可以帮助我修复正则表达式以使其在Java中用作字符串,还是可以帮助我创建一个可以工作的正则表达式?

EDIT 1: Here's the full String I am looking at that I am trying to match the JSON string above against: 编辑1:这是我正在尝试匹配上面的JSON字符串的完整字符串:

../../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. 编辑2:这是我用来执行正则表达式检查的Java。 The href variable contains the String content shown in edit 1. href变量包含编辑1中显示的String内容。

Pattern validationReportPattern = Pattern.compile(getValidationReportPattern());
Matcher validationReportMatcher = validationReportPattern.matcher(href);

public String getYangValidationReportPattern(){
   return "(\\[\\{.*\\}])"; 
}

String validationReport  = validationReportMatcher.group(1);

The regex pattern must be "(\\\\[\\\\{.*}])" in Java but the real problem is that no match has been attempted. 在Java中,正则表达式模式必须为"(\\\\[\\\\{.*}])" ,但真正的问题是未尝试匹配。 You have to call find() before calling group() . 您必须在调用group()之前调用find ()

If you do that it works, see here online at ideone . 如果您确实可行请在ideone在线查看

Output: 输出:

Match: [{"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"}]

The find() method returns a boolean so that you can check if there is an occurrence. find()方法返回一个布尔值,以便您可以检查是否存在该事件。 If you would not check that with find() first you would get an java.lang.IllegalStateException: No match found exception. 如果您首先不使用find()检查,则会得到java.lang.IllegalStateException: No match found异常。

if (validationReportMatcher.find())
{
   String validationReport  = validationReportMatcher.group(1);
   System.out.println ("Match: " + validationReport);
}
else
{
   System.out.println ("No match");
}

If you have to search for multiple matches you call find() in a while loop: 如果必须搜索多个匹配项,可以在while循环中调用find()

while (validationReportMatcher.find())
{
   String validationReport  = validationReportMatcher.group(1);
   System.out.println ("Match: " + validationReport);
}

But this seems not to be neccessary as you just look for one occurrence. 但这似乎不是必需的,因为您只需要查找一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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