匹配方法需要花费大量时间在正则表达式中执行

时间:2016-06-17 12:46:09

标签: java regex

使用名为glob的正则表达式,即*。

示例:

String str="this is *";

String str1="this is regular Expression";

如果我们在这些字符串上调用匹配项,则它们应该相等。

请检查以下代码,这些代码需要花费大量时间才能执行。

public static void main(String[] args) {

    String value="NO OTHER GERM CELL COMPONENT. TUMOR DIMENSIONS: **X**X** CM. TUMOR FOCALITY: FOCAL MULTIFOCAL INTRA-TUBULAR GERM CELL NEOPLASIA (ITGN): PRESENT ABSENT SCATTERED SYNCTOIOTROPHOBLASTIC GIANT CELLS PRESENT/NO SCATTERED SYNCYTIOTROPHOBLASTIC GIANT CELLS PRESENT RETE TESTIS INVOLVEMENT BY INVASIVE TUMOR: PRESENT ABSENT EXTENSION BEYOND TUNICA ALBUGINEA: PRESENT ABSENT ANGIOLYMPHATIC INVASION : PRESENT ABSENT SPERMATIC CORD MARGIN: POSITIVE NEGATIVE NECROSIS: PRESENT ABSENT SERUM TUMOR MARKERS: AVAILABLE NOT AVAILABLE PT**NXMXSX (AJCC 2010, SEVENTH EDITION) Appropriately controlled immunohistochemical stains for PLAP, OCT3/4, CD30, C-kit, and Pan cytokeratin were performed on a select representative block **# with following results: PLAP positive, OCT 3/4 positive, CD30 Negative, C-kit positive and Pan cytokeratin negative, supporting the above diagnosis.";
    String str=value;
    if (value.contains("*")) {
        value = value.replace("*", "[\\s\\S]*");
    }
    if (str.trim().matches(value)) {
        System.out.println("true");
    }else {
        System.out.println("false");
    }
}

1 个答案:

答案 0 :(得分:0)

使它如此慢的原因是你的字符串包含**,对于任何具有无限量词的内容,它都被两个匹配替换。

替换

value = value.replace("*", "[\\s\\S]*");

value = value.replace("**", "*").replace("*", "[\\s\\S]*");

你可以更快地得到你的结果。

请注意

String value="SOME (A) OTHER";
System.out.println(value.matches(value));

将始终打印为false - 正则表达式"SOME (A) OTHER"与自身不匹配,匹配"SOME A OTHER"