代码之家  ›  专栏  ›  技术社区  ›  Christian

Java中的元映射的正则表达式

  •  0
  • Christian  · 技术社区  · 15 年前

    元映射文件有以下行:

    mappings([map(-1000,[ev(-1000,'C0018017','Objective','Goals',[objective],[inpr],[[[1,1],[1,1],0]],yes,no)])]).
    

    格式解释为

    mappings(
          [map(negated overall score for this mapping, 
                [ev(negated candidate score,'UMLS concept ID','UMLS concept','preferred name for concept - may or may not be different',
                     [matched word or words lowercased that this candidate matches in the phrase - comma separated list],
                     [semantic type(s) - comma separated list],
                     [match map list - see below],candidate involved with head of phrase - yes or no,
                     is this an overmatch - yes or no
                   )
                ]
              )
          ]
        ).
    

    我想在Java中运行一个正则表达式查询,它给我字符串“UMLS概念ID”、语义类型和匹配映射列表。 ReGEX是正确的工具,还是用Java实现这个最有效的方法是什么?

    3 回复  |  直到 15 年前
        1
  •  3
  •   polygenelubricants    15 年前

    这是我对regex解决方案的尝试。这个 replace “元重组”方法是我正在试验的东西;我 希望 它可以读取更可读的代码。

    String line = "mappings([map(-1000,[ev(-1000,'C0018017','Objective','Goals',[objective],[inpr],[[[1,1],[1,1],0]],yes,no)])]).";
    String regex = 
        "mappings([map(number,[ev(number,<quoted>,quoted,quoted,[csv],[<csv>],[<matchmap>],yesno,yesno)])])."
        .replaceAll("([\\.\\(\\)\\[\\]])", "\\\\$1") // escape metacharacters
        .replace("<", "(").replace(">", ")") // set up capture groups
        .replace("number", "-?\\d+")
        .replace("quoted", "'[^']*'")
        .replace("yesno", "(?:yes|no)")
        .replace("csv", "[^\\]]*")
        .replace("matchmap", ".*?")
    ;
    System.out.println(regex);
    // prints "mappings\(\[map\(-?\d+,\[ev\(-?\d+,('[^']*'),'[^']*','[^']*',\[[^\]]*\],\[([^\]]*)\],\[(.*?)\],(?:yes|no),(?:yes|no)\)\]\)\]\)\."
    
    Matcher m = Pattern.compile(regex).matcher(line);
    if (m.find()) {
        System.out.println(m.group(1)); // prints "'C0018017'"
        System.out.println(m.group(2)); // prints "inpr"
        System.out.println(m.group(3)); // prints "[[1,1],[1,1],0]"
    }
    

    这个 代替 元重设允许您通过设置适当的 代替 (而不是把所有的东西都洒在一个不可读的地方)。

        2
  •  1
  •   Etaoin    15 年前

    这真是一种多毛的发型。Regex听起来很不错,但你会有一个真正多毛的Regex:

    mappings\(\[map\(-?[0-9.]+,\[ev\(-?[0-9.]+,'(.*?)','.*?','.*?',\[.*?\],\[(.*?)\],\[(.*)\],(?:yes|no),(?:yes|no)\)\]\)\]\)\.
    

    当你必须将正则表达式作为Java字符串来表达时,情况就变得更糟了。 \ 具有 \\ . 但这应该能得到你想要的;匹配组1、2和3是你想要拉出来的字符串。请注意,我并没有严格地测试它是否符合输入错误,因为我没有胃口。:)

    出于教育目的:尽管它的外观,但实际上并不难构建——我只是取了您的样本行,并用适当的通配符替换了实际值,确保避开括号和括号以及末尾的点。

        3
  •  1
  •   Jordan Stewart    15 年前

    有可能,是的。

    例如(假设您引用的值是唯一合法的引号,您添加的值是唯一合法的引号,值中不能有“[”和“]”字符,匹配映射列表中除了结尾之外不能有]]这样的内容。你得到了图片——很多假设。..)

    ^[^']+?'([^']*+)'[^\[]+\[[^]]+\],\[([^\]]*?)\],\[\[(.*?)\]\].*$
    

    它应该将这三个字段作为三个匹配的组提供给您(在您的示例中用 http://www.regexplanet.com/simple/index.html )

    哪个是-

    "^[^']+?'([^']*+)'[^\\[]+\\[[^]]+\\],\\[([^\\]]*?)\\],\\[\\[(.*?)\\]\\].*$"
    

    作为Java字符串。…

    但这不是很容易维护的。最好再详细一点!