iteye_7642 2009-02-20 21:19
浏览 300
已采纳

java正则表达式实现字符串匹配

问题描述:
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语句有些繁琐,是否有简单高效的实现方法呢?请指教,谢谢~

  • 写回答

5条回答 默认 最新

  • bohemia 2009-02-24 08:44
    关注

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

    上面写错了个变量名. ..这样才对.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮