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

huangapple go评论113阅读模式
英文:

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

问题

以下是字段的名称翻译:

  1. "{
  2. account_number={
  3. type=long
  4. },
  5. firstname={
  6. type=text, fields={
  7. keyword={
  8. ignore_above=256, type=keyword
  9. }
  10. }
  11. },
  12. accountnumber={
  13. type=long
  14. },
  15. address={
  16. type=text, fields={
  17. keyword={
  18. ignore_above=256, type=keyword
  19. }
  20. }
  21. },
  22. gender={
  23. type=text, fields={
  24. keyword={
  25. ignore_above=256, type=keyword
  26. }
  27. }
  28. }
  29. }"

需要获取的字段名称是:account_number, firstname, accountnumber, address, gender。由于对象内部的内容不固定,因此无法使用POJO类。正则表达式可能会起作用。有什么建议吗?

英文:

String is as follows :

  1. "{
  2. account_number={
  3. type=long
  4. },
  5. firstname={
  6. type=text, fields={
  7. keyword={
  8. ignore_above=256, type=keyword
  9. }
  10. }
  11. },
  12. accountnumber={
  13. type=long
  14. },
  15. address={
  16. type=text, fields={
  17. keyword={
  18. ignore_above=256, type=keyword
  19. }
  20. }
  21. },
  22. gender={
  23. type=text, fields={
  24. keyword={
  25. ignore_above=256, type=keyword
  26. }
  27. }
  28. }
  29. }"

I need to get only the names of these fields, i.e, 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 ?

答案1

得分: 1

  1. import java.util.HashSet;
  2. import java.util.Set;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import org.json.JSONObject;
  6. public class SOTest {
  7. public static void main(String args[]) {
  8. Set<String> keywords = new HashSet<String>();
  9. final String regex = "[a-z]\\w*";
  10. String string = "{
  11. account_number={
  12. type=long
  13. },
  14. firstname={
  15. type=text, fields={
  16. keyword={
  17. ignore_above=256, type=keyword
  18. }
  19. }
  20. },
  21. accountnumber={
  22. type=long
  23. },
  24. address={
  25. type=text, fields={
  26. keyword={
  27. ignore_above=256, type=keyword
  28. }
  29. }
  30. },
  31. gender={
  32. type=text, fields={
  33. keyword={
  34. ignore_above=256, type=keyword
  35. }
  36. }
  37. }
  38. }";
  39. final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  40. final Matcher matcher = pattern.matcher(string);
  41. while(matcher.find()) {
  42. String gp = matcher.group();
  43. keywords.add(gp);
  44. }
  45. for (String keyword : keywords) {
  46. string = string.replace(keyword, "\"" + keyword + "\"");
  47. }
  48. string = string.replace("=", ":");
  49. System.out.println(string);
  50. JSONObject jsonObject = new JSONObject(string);
  51. System.out.println(jsonObject.keySet());
  52. }
  53. }
英文:

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

  1. import java.util.HashSet;
  2. import java.util.Set;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import org.json.JSONObject;
  6. public class SOTest {
  7. public static void main(String args[]) {
  8. Set&lt;String&gt; keywords = new HashSet&lt;String&gt;();
  9. final String regex = &quot;[a-z]\\w*&quot;;
  10. String string = &quot;{\n&quot;
  11. + &quot; account_number={\n&quot;
  12. + &quot; type=long\n&quot;
  13. + &quot; },\n&quot;
  14. + &quot; firstname={\n&quot;
  15. + &quot; type=text, fields={\n&quot;
  16. + &quot; keyword={\n&quot;
  17. + &quot; ignore_above=256, type=keyword\n&quot;
  18. + &quot; }\n&quot;
  19. + &quot; }\n&quot;
  20. + &quot; },\n&quot;
  21. + &quot; accountnumber={\n&quot;
  22. + &quot; type=long\n&quot;
  23. + &quot; },\n&quot;
  24. + &quot; address={\n&quot;
  25. + &quot; type=text, fields={\n&quot;
  26. + &quot; keyword={\n&quot;
  27. + &quot; ignore_above=256, type=keyword\n&quot;
  28. + &quot; }\n&quot;
  29. + &quot; }\n&quot;
  30. + &quot; },\n&quot;
  31. + &quot; gender={\n&quot;
  32. + &quot; type=text, fields={\n&quot;
  33. + &quot; keyword={\n&quot;
  34. + &quot; ignore_above=256, type=keyword\n&quot;
  35. + &quot; }\n&quot;
  36. + &quot; }\n&quot;
  37. + &quot; }\n&quot;
  38. + &quot;}&quot;;
  39. final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  40. final Matcher matcher = pattern.matcher(string);
  41. while(matcher.find()) {
  42. String gp = matcher.group();
  43. keywords.add(gp);
  44. }
  45. for (String keyword : keywords) {
  46. string = string.replace(keyword, &quot;\&quot;&quot;+keyword+&quot;\&quot;&quot;);
  47. }
  48. string = string.replace(&quot;=&quot;, &quot;:&quot;);
  49. System.out.println(string);
  50. JSONObject jsonObject = new JSONObject(string);
  51. System.out.println(jsonObject.keySet());
  52. }
  53. }

output

  1. [account_number, firstname, accountnumber, address, gender]

huangapple
  • 本文由 发表于 2020年5月5日 00:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61596927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定