[英]How to extract specific text from string using Regex in JAVA
I have a String = '["Id","Lender","Type","Aging","Override"]'
我有一个String =
'["Id","Lender","Type","Aging","Override"]'
from which I want to extract Id, Lender, Type and so on in an String array. 从中我想在String数组中提取Id,Lender,Type等。 I am trying to extract it using Regex but, the pattern is not removing the "[".
我试图使用正则表达式提取它,但模式不是删除“[”。
Can someone please guide me. 有人可以指导我。 Thanks!
谢谢!
Update: code I tried, 更新:我试过的代码,
Pattern pattern = Pattern.compile("\"(.+?)\"");
Matcher matcher = pattern.matcher(str);
List<String> list = new ArrayList<String>();
while (matcher.find()) {
// System.out.println(matcher.group(1));.
list.add(matcher.group(1));
(Ps: new to Regex) (Ps:Regex的新手)
but if your input was, say: 但如果您的意见是,请说:
["Id","Lender","Ty\"pe","Aging","Override", "Override\\\\\"\""]
this regex will capture all values, while allowing those (valid) escaped quotes \\"
and literal backslashes \\\\
in your strings 这个正则表达式将捕获所有值,同时允许字符串中的那些(有效)转义引号
\\"
和文字反斜杠\\\\
regex: "((?:\\\\\\\\|\\\\"|[^"])+)"
正则表达式:
"((?:\\\\\\\\|\\\\"|[^"])+)"
or as java string: "\\"((?:\\\\\\\\\\\\\\\\|\\\\\\\\\\"|[^\\"])+)\\""
或者作为java字符串:
"\\"((?:\\\\\\\\\\\\\\\\|\\\\\\\\\\"|[^\\"])+)\\""
You can do something like this. 你可以做这样的事情。 It first removes
"[ ]"
and then splits on ","
它首先删除
"[ ]"
,然后拆分","
System.out.println(Arrays.toString(string.replaceAll("\\[(.*)\\]", "$1").split(",")));
Hope this helps. 希望这可以帮助。
Your code works, I tried it and I got the output you want. 你的代码工作,我尝试了,我得到了你想要的输出。
String line = "[\"Id\",\"Lender\",\"Type\",\"Aging\",\"Override\"]";
Pattern r = Pattern.compile("\"(.+?)\"");
List<String> result = new ArrayList<>();
// Now create matcher object.
Matcher m = r.matcher(line);
while (m.find( )) {
result.add(m.group(1));
}
System.out.println(result);
output: 输出:
[Id, Lender, Type, Aging, Override]
obviously the square brackets are there because I am printing a List
, they are not part of the words. 显然方括号是因为我打印一个
List
,它们不是单词的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.