解析Java中的带引号的文本 [英] Parsing quoted text in java
问题描述
是否有一种简单的方法可以将带引号的文本解析为java的字符串?我有这样的行要解析:
Is there an easy way to parse quoted text as a string to java? I have this lines like this to parse:
author="Tolkien, J.R.R." title="The Lord of the Rings"
publisher="George Allen & Unwin" year=1954
我想要的只是Tolkien,J.R.R.,《指环王》,乔治·艾伦(George Allen)和Unwin,1954年作弦乐.
and all I want is Tolkien, J.R.R.,The Lord of the Rings,George Allen & Unwin, 1954 as strings.
推荐答案
您可以使用正则表达式,例如
You could either use a regex like
"(.+)"
它将匹配引号之间的任何字符.在Java中将是:
It will match any character between quotes. In Java would be:
Pattern p = Pattern.compile("\\"(.+)\\"";
Matcher m = p.matcher("author=\"Tolkien, J.R.R.\"");
while(matcher.find()){
System.out.println(m.group(1));
}
请注意,使用的是group(1),这是第二个匹配项,第一个匹配的是group(0),是带引号的完整字符串
Note that group(1) is used, this is the second match, the first one, group(0), is the full string with quotes
在场外,您还可以使用子字符串选择除第一个和最后一个字符以外的所有内容:
Offcourse you could also use a substring to select everything except the first and last char:
String quoted = "author=\"Tolkien, J.R.R.\"";
String unquoted;
if(quoted.indexOf("\"") == 0 && quoted.lastIndexOf("\"")==quoted.length()-1){
unquoted = quoted.substring(1, quoted.lenght()-1);
}else{
unquoted = quoted;
}
这篇关于解析Java中的带引号的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!