0

问题:

在 Java 中,我将解析脚本并从文件中提取以

    ${GLOBAL_} or ${AUTO_}

我想拾取所有东西,直到最后一个花括号,例如,如果我有以下字符串:

    "this is a String ${AUTO_TEST_f} body ${GLOBAL_SYNC} ${AUTO_2} ${OTHER_VAR}"

结果应该是:

${AUTO_TEST_f} 

${GLOBAL_SYNC}

${AUTO_2} 

我尝试过的:

我试图创建正则表达式模式(我相信它确实有效)并使用它们来创建匹配器。然后我尝试使用 Matcher 将所有匹配项打印到控制台,但我遇到了一些问题。出于某种原因,它跳过了 ${GLOBAL_VARIABLE_1}。另外,我怎样才能实现这个给我所有的比赛?一个while循环[while match.group(0) != null]?

这是我的代码:

 String re1="(\\$)";    // Any Single Character 1
String re2="(\\{)"; // Any Single Character 2
String re3="(G)";   // Any Single Character 3
String re4="(L)";   // Any Single Character 4
String re5="(O)";   // Any Single Character 5
String re6="(B)";   // Any Single Character 6
String re7="(A)";   // Any Single Character 7
String re8="(L)";   // Any Single Character 8
String re9="(_)";   // Any Single Character 9
String re10="(.*?)";    // Any Single Character 10
String re11="(\\})";    // Any Single Character 11


String r1="(\\$)";  // Any Single Character 1
String r2="(\\{)";  // Any Single Character 2
String r3="(A)";    // Any Single Character 3
String r4="(U)";    // Any Single Character 4
String r5="(T)";    // Any Single Character 5
String r6="(O)";    // Any Single Character 6
String r7="(_)";    // Any Single Character 7
String r8="(.*?)";  // Any Single Character 8
String r9="(\\})";  // Any Single Character 9

Pattern p = Pattern.compile((re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11));
Pattern p2 = Pattern.compile(r1+r2+r3+r4+r5+r6+r7+r8+r9);

 Matcher m = p.matcher(txt);
 Matcher m1 = p2.matcher(txt);
 m1.find();
 System.out.println(m1.group(0));
 m.find();
 System.out.println(m.group(0));

这是控制台结果:

 Actual Results:
 ${AUTO_1}
 ${GLOBAL_VARIABLE_2}

这是我的预期结果:

 Expected Results:
 ${GLOBAL_VARIABLE_1}
 ${AUTO_1}
 ${GLOBAL_VARIABLE_2}
 ${GLOBAL_VARIABLE_3}

谢谢!

4

3 回答 3

4

不要过于复杂:

String txt = "this is a String ${AUTO_TEST_f} body ${GLOBAL_SYNC} ${AUTO_2}";
String regex = "\\$\\{(AUTO|GLOBAL)_(.*?)\\}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(txt);
while (matcher.find()) {
    System.out.println(matcher.group() + "\t->\t" + matcher.group(2) + "\t(" + matcher.group(1) + ")" );
}

输出:

${AUTO_TEST_f}  ->  TEST_f  (AUTO)
${GLOBAL_SYNC}  ->  SYNC    (GLOBAL)
${AUTO_2}       ->  2       (AUTO)
于 2013-04-09T17:19:05.207 回答
1

试试这个:

String data = "this is a String ${AUTO_TEST_f} body ${GLOBAL_SYNC} ${AUTO_2}";
    Pattern pattern = Pattern.compile("\\$\\{.+?\\}");

    Matcher matcher = pattern.matcher(data);

    while (matcher.find()) {
        // Indicates match is found. Do further processing
        System.out.println(matcher.group());
    }

输出是:

${AUTO_TEST_f}
${GLOBAL_SYNC}
${AUTO_2}

于 2013-04-09T17:14:45.247 回答
1

无论您在做什么,这都不是正确的编码方式。从较小的组件组​​成一个正则表达式很好,但是当您将组件分解为单个字符时它是没有意义的。

如果你想得到那些以GLOBALor开头的AUTO,它很简单:

\$\{(GLOBAL|AUTO)_.*?\}

将正则表达式放入字符串文字中:

"\\$\\{(GLOBAL|AUTO)_.*?\\}"
于 2013-04-09T17:16:14.583 回答