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评论86阅读模式
英文:

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

问题

以下是字段的名称翻译:

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

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

英文:

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, 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

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 = "{
            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
            }
            }
            }
        }";
        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());
    }
}
英文:

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&lt;String&gt; keywords = new HashSet&lt;String&gt;();
final String regex = &quot;[a-z]\\w*&quot;;
String string = &quot;{\n&quot;
+ &quot;    account_number={\n&quot;
+ &quot;    type=long\n&quot;
+ &quot;    },\n&quot;
+ &quot;    firstname={\n&quot;
+ &quot;    type=text, fields={\n&quot;
+ &quot;    keyword={\n&quot;
+ &quot;    ignore_above=256, type=keyword\n&quot;
+ &quot;            }\n&quot;
+ &quot;        }\n&quot;
+ &quot;    },\n&quot;
+ &quot;    accountnumber={\n&quot;
+ &quot;    type=long\n&quot;
+ &quot;    },\n&quot;
+ &quot;    address={\n&quot;
+ &quot;    type=text, fields={\n&quot;
+ &quot;    keyword={\n&quot;
+ &quot;    ignore_above=256, type=keyword\n&quot;
+ &quot;            }\n&quot;
+ &quot;        }\n&quot;
+ &quot;    },\n&quot;
+ &quot;    gender={\n&quot;
+ &quot;    type=text, fields={\n&quot;
+ &quot;    keyword={\n&quot;
+ &quot;    ignore_above=256, type=keyword\n&quot;
+ &quot;            }\n&quot;
+ &quot;        }\n&quot;
+ &quot;    }\n&quot;
+ &quot;}&quot;;
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, &quot;\&quot;&quot;+keyword+&quot;\&quot;&quot;);
}
string = string.replace(&quot;=&quot;, &quot;:&quot;);
System.out.println(string);
JSONObject jsonObject = new JSONObject(string);
System.out.println(jsonObject.keySet());
}
}

output

[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:

确定