匹配包含完整字符串中的分隔符的子字符串

时间:2017-01-11 15:25:21

标签: java regex pattern-matching match

我不确定如何说出这个问题。长话短说,我想从行a中提取两个字符串(bIn: a (b))。在几乎所有情况下a=b,但为了以防万一,我已将它们分开了。问题:两个字符串都可以包含任何字符,包括Unicode,空格,标点符号和括号。

1: In: ThisName (ThisName) is in this list
2: In: OtherName (With These) (OtherName (With These)) is in this list
3: In: Really Annoying (Because) Separators (Really Annoying (Because) Separators) is in this list

第1行,简单:^\w+:\s(?'a'.+?)\s\((?'b'.+)\) a:ThisName b:ThisName

第2行,与之前相同:a:OtherName b: With These) (OtherName (With These)

第2行,懒惰:^\w+:\s(?'a'.+?)\s\((?'b'.+?)\) a:OtherName b:With These

第3行,总台

这可能吗?也许我需要走另一条路?我们知道需要一组括号。也许我必须沿着数学路线,计算括号的数量并找到该路线以确定哪个应该实际包含b?以某种方式计算每次打开和关闭。

我一直在玩的是:https://regex101.com/r/8YIweJ/2

顺便说一句,如果我可以改变输入格式,我肯定会。

补充问题:如果无法做到这一点,是否一直假设a=b使这更容易?我无法想象它会如何。

3 个答案:

答案 0 :(得分:0)

我要做的是不使用正则表达式。遵循这种算法:

  1. 找到第一个索引(如果我按照你的问题,应该给你#34; a"字符串
  2. 从该索引使用charAt按字符逐个字符串。当你击中a时向上计数(当你到达a时向下计数)。一旦你在这个计数器中达到零,那么你的括号就会匹配,并且你有" b"的结尾位置。字符串。
  3. 看起来也可能有多个字符串组成" B" (从第3行开始),所以你可以按照上面的第2步继续迭代字符串,将字符串添加到列表或字符串构建器中。

答案 1 :(得分:0)

我的评论嵌入在processInput方法中。

public static void main(String[] args)
{
    String input = "1: In: ThisName (ThisName) is in this list\n" +
        "2: In: OtherName (With These) (OtherName (With These)) is in this list\n" +
        "3: In: Really Annoying (Because) Separators (Really Annoying (Because) Separators) is in this list\n" +
        "4: In: Not the Same (NotTheSame) is in this list\n" +
        "5: In: A = (B) (A = (B)) is in this list\n" +
        "6: In: A != (B) (A != B) is in this list\n";

    for (String line : input.split("\n"))
    {
        processInput(line);
    }
}


public static void processInput(String line)
{
    // Parse the relevant part from the input.
    Matcher inputPattern = Pattern.compile("(\\d+): In: (.*) is in this list").matcher(line);
    if (!inputPattern.matches())
    {
        System.out.println(line + " is not valid input");
        return;
    }
    String inputNum = inputPattern.group(1);
    String aAndB = inputPattern.group(2);

    // Check if a = b.
    Matcher aEqualsBPattern = Pattern.compile("(.*) \\(\\1\\)").matcher(aAndB);
    if (aEqualsBPattern.matches())
    {
        System.out.println("Input " + inputNum + ":");
        System.out.println("a = b = " + aEqualsBPattern.group(1));
        System.out.println();
        return;
    }

    // Check if a and b have no parentheses.
    Matcher noParenthesesPattern = Pattern.compile("([^()]*) \\(([^()]*)\\)").matcher(aAndB);
    if (noParenthesesPattern.matches())
    {
        System.out.println("Input " + inputNum + ":");
        System.out.println("a = " + noParenthesesPattern.group(1));
        System.out.println("b = " + noParenthesesPattern.group(2));
        System.out.println();
        return;
    }

    // a and b have one or more parentheses in them.
    // All you can do now is guess what a and b are.

    // There is at least one " (" in the string.
    String[] split = aAndB.split(" \\(");
    for (int i = 0; i < split.length - 1; i++)
    {
        System.out.println("Possible Input " + inputNum + ":");
        System.out.println("possible a = " + mergeParts(split, 0, i));
        System.out.println("possible b = " + mergeParts(split, i + 1, split.length - 1));
        System.out.println();
    }
}


private static String mergeParts(String[] aAndBParts, int startIndex, int endIndex)
{
    StringBuilder s = new StringBuilder(getPart(aAndBParts, startIndex));
    for (int j = startIndex + 1; j <= endIndex; j++)
    {
        s.append(" (");
        s.append(getPart(aAndBParts, j));
    }
    return s.toString();
}


private static String getPart(String[] aAndBParts, int j)
{
    if (j != aAndBParts.length - 1)
    {
        return aAndBParts[j];
    }
    return aAndBParts[j].substring(0, aAndBParts[j].length() - 1);
}

执行上述代码输出:

Input 1:
a = b = ThisName

Input 2:
a = b = OtherName (With These)

Input 3:
a = b = Really Annoying (Because) Separators

Input 4:
a = Not the Same
b = NotTheSame

Input 5:
a = b = A = (B)

Possible Input 6:
possible a = A !=
possible b = B) (A != B

Possible Input 6:
possible a = A != (B)
possible b = A != B

答案 2 :(得分:0)

好吧,您可以解析文本,但不能使用正则表达式,并且至少满足下列条件之一:

  1. 保证B表达式中的括号正确匹配。也就是说,没有)) ((:-)
  2. A和B完全一样。在这种情况下,即使您在其中有不匹配的括号,例如Hello (-: (Hello (-:),您知道第二个(之前的Hello是&#34;权利&#34;之一。
  3. 如果您无法做出这些保证,那么您应该编写一个isMatchedParenthesis(String)方法,检查所有括号是否正确匹配。有一个计数器,从零开始,扫描字符串。

    • 对于字符串中的每个字符:
      • 如果当前字符为(counter++
      • 如果当前字符为)counter--
      • 如果计数器为负数,则返回false
    • 如果计数器结束时为正,则返回false。否则是真的。

    使用该方法测试您的字符串。如果它有效,你可以依靠找到&#34;重要的&#34;括号匹配的括号。如果返回false,则可以尝试使用假定两个字符串相同的回退方法。

    在平衡时找到重要的括号

    • 找到最右边的)索引(使用lastIndexOf)。
    • counter=0
    • 对于从该索引向下的每个字符到4(In:之后的字符:
      • 如果是)counter++
      • 如果是(counter--
      • 如果counter==0停止,则返回当前索引。

    现在你有了重要括号的索引。你的A是4和这个索引之间的子串 - 1(记住(之前的空格)。你的B是从那个索引+ 1到你先找到的右)的索引。

    后备法

    假设您的括号不平衡。你能做点什么吗?

    • 列出字符串中(的所有索引。
    • 如果列表的长度是偶数 - 字符串错误,请向用户报告。
    • 如果长度为奇数,请取中间(的索引。假设A和B相同,则它们应该具有相同数量的(,因此左侧和右侧具有相同数量(的那个是您的候选者。
    • 像以前一样提取A和B.如果它们不相等 - 错误的字符串,请向用户报告。
相关问题