问题描述:
resultStr = "@inproceedings{1031197, author = {S. Alireza Aghili and Divyakant Agrawal and Amr El Abbadi}, title = {Protein structure alignment using geometrical features}, booktitle = {CIKM '04: Proceedings of the thirteenth ACM international conference on Information and knowledge management}, year = {2004}, isbn = {1-58113-874-1}, pages = {148--149}, location = {Washington, D.C., USA}, doi = {http://doi.acm.org/10.1145/1031171.1031197}, publisher = {ACM}, address = {New York, NY, USA}, },"
要将resultStr中{}内部的内容识别出来,并与 = 前的内容匹配。即得到
author的值为S. Alireza Aghili and Divyakant Agrawal and Amr El Abbadi,
title的值为Protein structure alignment using geometrical features,等等。
我的做法有些问题,运行结果不正确,请大家指点,谢谢!
/**
* 对BitTex输出格式识别
*
*/
public static GlobalMode bitTex_identify(String resultStr) {
GlobalMode obj = new GlobalMode();
Pattern p = Pattern.compile("(\\s[\\w]+\\s=)(\\s\\{.*?},)");
Matcher m = p.matcher(resultStr);
String reStr[] = new String[100];
int i = 0;
while (m.find()) {
i++;
reStr[i] = (m.group(1) + m.group(2)).trim();
String value = reStr[i].substring(reStr[i].indexOf("{") + 1,
reStr[i].lastIndexOf("}"));
if(reStr[i].startsWith("author")){
obj.setAuthor(value);
continue;
}
if (reStr[i].startsWith("title")) {
obj.setTitle(value);
continue;
}
if (reStr[i].startsWith("journal")) {
obj.setJournal(value);
continue;
}
if (reStr[i].startsWith("booktitle")) {
obj.setBooktitle(value);
continue;
}
if (reStr[i].startsWith("year")) {
obj.setYear(value);
continue;
}
if (reStr[i].startsWith("month")) {
obj.setMonth(value);
continue;
}
if (reStr[i].startsWith("pages")) {
obj.setPages(value);
continue;
}
if (reStr[i].startsWith("volume")) {
obj.setVolume(value);
continue;
}
if (reStr[i].startsWith("number")) {
obj.setNumber(value);
continue;
}
if (reStr[i].startsWith("publisher")) {
obj.setPublisher(value);
continue;
}
if (reStr[i].startsWith("location")) {
obj.setLocation(value);
continue;
}
if (reStr[i].startsWith("addresultStrss")) {
obj.setAddress(value);
continue;
}
if (reStr[i].startsWith("isbn")) {
obj.setIsbn(value);
continue;
}
if (reStr[i].startsWith("doi")) {
obj.setDoi(value);
continue;
}
if(reStr[i].startsWith("issue_description")){
obj.setTitle(value);
continue;
}
if(reStr[i].startsWith("issue_date")){
obj.setYear(value);
//obj.setYear(value.substring(value.indexOf(" ")));
continue;
}
}
return obj;
}
补充: 1、感觉这么写if语句太繁琐,但不知道怎么改。
2、程序其它地方要用到返回的author,title等值,如:要用参考文献的格式将这些值打印出来。
[b]问题补充:[/b]
不好意思,之前我说的问题不清楚。我是在得到这样的数据之后需要再处理
author =S. Alireza Aghili and Divyakant Agrawal and Amr El Abbadi
title =Protein structure alignment using geometrical features
booktitle =CIKM '04: Proceedings of the thirteenth ACM international conference on Information and knowledge management
year =2004
...
最终目的是能随意调用author,title 这些值。如:要用参考文献的格式将这些值打印出来。
[b]问题补充:[/b]
您所说的数据项是指author这些么?
那是不是还要用if-else语句来判断?
[b]问题补充:[/b]
Sorry,我总是表达的不够清楚。
我是想定义一个实例obj,将author,title这些对应的值分别赋值给obj.author, obj.title等,最后可以随意调用obj.author, obj.title。
我的代码基本上能实现这个功能,但是觉得用这么多的if-else语句有些繁琐,是否有简单高效的实现方法呢?请指教,谢谢~
[code="java"]String resultStr ="字符串";
Pattern p = Pattern.compile("[\s\t]+([\w\s]+)=[\s]+\{([^}]+)\}");
Map tmpMap = new HashMap();
Matcher m = p.matcher(resultStr);
while(m.find()){
String key=m.group(1);
String value=m.group(2);
System.out.println(key+"="+value);
tmpMap.put(key,value);
}
GlobalMode obj = new GlobalMode();
obj.setAuthor(tmpMap.get("author"));
obj.setTitle(tmpMap.get("title"));
.... [/code]
上面写错了个变量名. ..这样才对.
正则应该可以,给你写了个.你试试看.
[code="java"] String resultStr = "@inproceedings{1031197, author = {S. Alireza Aghili and Divyakant Agrawal and Amr El Abbadi}, title = {Protein structure alignment using geometrical features}, booktitle = {CIKM '04: Proceedings of the thirteenth ACM international conference on Information and knowledge management}, year = {2004}, isbn = {1-58113-874-1}, pages = {148--149}, location = {Washington, D.C., USA}, doi = {http://doi.acm.org/10.1145/1031171.1031197}, publisher = {ACM}, address = {New York, NY, USA}, }," ;
Pattern p = Pattern.compile("[\s\t]+([\w\s]+)=[\s]+\{([^}]+)\}");
Matcher m = p.matcher(resultStr);
while(m.find()){
System.out.println(m.group(1)+"="+m.group(2));
}[/code]
1.正则表达式匹配所有的数据项;
2.数据项内容的Key存储到HashMap中;
其他地方随意根据key获取就可以了吧.
[quote]问题补充:
您所说的数据项是指author这些么?
那是不是还要用if-else语句来判断? [/quote]
我不太确定你想要做什么.
[code="java"]String resultStr ="字符串";
Pattern p = Pattern.compile("[\s\t]+([\w\s]+)=[\s]+\{([^}]+)\}");
Map tmpMap = new HashMap();
Matcher m = p.matcher(resultStr);
while(m.find()){
String key=m.group(1);
String value=m.group(2);
System.out.println(key+"="+value);
m.put(key,value);
}
GlobalMode obj = new GlobalMode();
obj.setAuthor(m.get("author"));
obj.setTitle(m.get("title"));
....[/code]
这样不行? 不明白为什么非要添加 if -else?