简体   繁体   English

使用正则表达式在 JSON 对象中查找密码值

[英]Find passwords values in JSON objects using Regex

I have a big JSON object which contains a lot of different JSON, most of them have the structure below (key: sometext-v1.password, and value: password for example:我有一个包含许多不同 JSON 的大 JSON 对象,其中大多数具有以下结构(例如:key: sometext-v1.password, and value: password:

"apps":[
        "settings": [
                    {
                       "name" : "sometext-v1.password-v1",
                       "value" : "myPassword"
                    },
                    ...
                    ...

I want to use Regex to extract all passwords by a name which contains 'password' string and its value, but I don't want to iterate the JSON name by name because this takes a lot of time for processing.我想使用正则表达式按包含“密码”字符串及其值的名称提取所有密码,但我不想按名称迭代 JSON 名称,因为这需要大量时间进行处理。

I tried this, but it isn't working:我试过这个,但它不起作用:

String regex = "\"*password*\":\\s*\".*?\"";

You can use您可以使用

String regex = "\"name\"\\s*:\\s*\"[^\"]*password[^\"]*\",\\s*\"value\"\\s*:\\s*\"([^\"]*)";

See the regex demo .请参阅正则表达式演示 Details:细节:

  • "name" - a literal string "name" - 一个文字字符串
  • \\s*:\\s* - a : enclosed with optional whitespaces \\s*:\\s* - a :用可选的空格括起来
  • " - a " char " - 一个"字符
  • [^"]*password[^"]* - password enclosed with 0 or more chars other than a " [^"]*password[^"]* - password包含 0 个或多个字符而不是"
  • ", - a ", string ", - a ",字符串
  • \\s* - zero or more whitespaces \\s* - 零个或多个空格
  • "value" - a literal text "value" - 文字文本
  • \\s*:\\s* - a : enclosed with optional whitespaces \\s*:\\s* - a :用可选的空格括起来
  • " - a " char " - 一个"字符
  • ([^"]*) - Group 1: any zero or more chars other than " . ([^"]*) - 第 1 组:除"之外的任何零个或多个字符。

See a Java demo:看一个 Java 演示:

String s = "\"apps\":[\n        \"settings\": [\n                    {\n                       \"name\" : \"sometext-v1.password-v1\",\n                       \"value\" : \"myPassword\"\n                    },\n                    ...";
String regex = "\"name\"\\s*:\\s*\"[^\"]*password[^\"]*\",\\s*\"value\"\\s*:\\s*\"([^\"]*)";
Matcher m = Pattern.compile(regex).matcher(s);
if (m.find()) {
    System.out.println(m.group(1));
}
// => myPassword

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

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