Java 如何使用regex从以下字符串中以对象格式(不使用POJO)仅获取给定字符串中的字段名

Java 如何使用regex从以下字符串中以对象格式(不使用POJO)仅获取给定字符串中的字段名,java,regex,string,regex-group,Java,Regex,String,Regex Group,字符串如下所示: "{ account_number={ type=long }, firstname={ type=text, fields={ keyword={ ignore_above=256, type=keyword } } }, accountnumber={ type=long }, address={ type=text, fields

字符串如下所示:

"{
    account_number={
    type=long
    },
    firstname={
    type=text, fields={
    keyword={
    ignore_above=256, type=keyword
            }
        }
    },
    accountnumber={
    type=long
    },
    address={
    type=text, fields={
    keyword={
    ignore_above=256, type=keyword
            }
        }
    },
    gender={
    type=text, fields={
    keyword={
    ignore_above=256, type=keyword
            }
        }
    }
}"

我只需要获取这些字段的名称,即帐号、名字、帐号、地址、性别。Pojo类在这里不起作用,因为对象中的内容是不固定的。注册医生可以工作。有什么建议吗?

在这里,我已经将您的字符串转换为JSON,然后检索了所有键

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;


public class SOTest {

    public static void main(String args[]) {
        Set<String> keywords = new HashSet<String>();
        final String regex = "[a-z]\\w*";
        String string = "{\n"
             + "    account_number={\n"
             + "    type=long\n"
             + "    },\n"
             + "    firstname={\n"
             + "    type=text, fields={\n"
             + "    keyword={\n"
             + "    ignore_above=256, type=keyword\n"
             + "            }\n"
             + "        }\n"
             + "    },\n"
             + "    accountnumber={\n"
             + "    type=long\n"
             + "    },\n"
             + "    address={\n"
             + "    type=text, fields={\n"
             + "    keyword={\n"
             + "    ignore_above=256, type=keyword\n"
             + "            }\n"
             + "        }\n"
             + "    },\n"
             + "    gender={\n"
             + "    type=text, fields={\n"
             + "    keyword={\n"
             + "    ignore_above=256, type=keyword\n"
             + "            }\n"
             + "        }\n"
             + "    }\n"
             + "}";
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
         while(matcher.find()) {
             String gp = matcher.group();
             keywords.add(gp);
         }
         for (String keyword : keywords) {
            string = string.replace(keyword, "\""+keyword+"\"");
        }
         string = string.replace("=", ":");
         System.out.println(string);
        JSONObject jsonObject = new JSONObject(string);
        System.out.println(jsonObject.keySet());
    }
}

该示例不是有效的JSON,请检查它是字符串还是JSONHi@Hades:它只是一个字符串。这就是问题所在。一些我需要的注册表项,它只能读取字段名account_number、firstname、accountnumber、address、genderThanks,用于回复@Hades。这个解决方案的问题是“filed”和“keywords”不是常量。它可以是任何东西。例如:{city:{type:text,字段:{raw:{type:keyword}}}}}@ishita07检查我的更新答案。让我知道这是否有效。@ishita07如果你认为这是对你的问题最合适的选择,请你投赞成票。对不起,昨天没有投票。刚刚验证:)再次感谢!:)
[account_number, firstname, accountnumber, address, gender]