简体   繁体   中英

How to get just the field names from the given string in object format ( not using POJO ) from the following string using regex

String is as follows:

"{
    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
            }
        }
    }
}"

I need to get only the names of these fields, ie, account_number,firstname,accountnumber,address,gender. Pojo class won't work here since the content inside objects is not fixed. A reg ex may work. Any suggestions?

Here I have converted ur string into JSON and then retrieved all the keys

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());
    }
}

output

[account_number, firstname, accountnumber, address, gender]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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