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

Java 匹配包含完整字符串分隔符的子字符串,java,regex,pattern-matching,match,Java,Regex,Pattern Matching,Match,我不知道该如何表达这个问题。长话短说,我想从a(b)中的行中拉出两个字符串(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 (Bec

我不知道该如何表达这个问题。长话短说,我想从a(b)中的
行中拉出两个字符串(
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 this)(OtherName(With this)

第2行,lazy:
^\w+:\s(?'a'.+?)\s\((?'b'.+?)\)
a:OtherName
b:With the this

3号线,总台

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

我一直在玩的东西:

顺便说一下,如果我能改变输入格式,我肯定会的


补充问题:如果这不可能,那么一直假设
a=b
会让这更容易吗?我想不出会是什么样子。

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

  • 找到(的)的第一个索引,如果我遵循您的问题,该索引将为您提供“a”字符串
  • 从该索引开始,使用charAt逐个字符地遍历字符串。当你点击a时向上计数(到达a时向下计数)。当你在该计数器中点击零时,括号匹配,你就得到了“b”字符串末尾的位置

  • 看起来可能有多个字符串组成“B”(从第3行开始),因此您可以按照上面的步骤2不断迭代字符串,将字符串添加到列表或字符串生成器中(视情况而定)。

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

  • 找到(的)的第一个索引,如果我遵循您的问题,该索引将为您提供“a”字符串
  • 从该索引开始,使用charAt逐个字符地遍历字符串。当你点击a时向上计数(到达a时向下计数)。当你在该计数器中点击零时,括号匹配,你就得到了“b”字符串末尾的位置

  • 看起来可能有多个字符串组成“B”(从第3行开始),因此您可以按照上面的步骤2不断迭代字符串,将字符串添加到列表或字符串生成器中(视情况而定)。

    我的注释嵌入到
    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);
    }
    

    我的注释嵌入在
    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);
    }
    

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

  • B表达式中的括号保证正确匹配。即,无
    )((
    :-)
    )等
  • A和B完全相同。在这种情况下,即使括号中有不匹配的括号,例如
    Hello(-::)
    ,您也知道第二个
    Hello
    之前的
    是“正确”的括号
  • 如果您不能做出这些保证,那么您应该编写一个
    isMatchedBranchers(String)
    方法,检查所有括号是否正确匹配。从零开始设置一个计数器,并扫描整个字符串

    • 对于字符串中的每个字符:
      • 如果当前字符是
        计数器+
      • 如果当前字符为
        计数器--
      • 如果计数器为负数,则返回false
    • 如果最后计数器为正,则返回false。否则返回true
    使用该方法测试字符串。如果有效,则可以使用括号匹配查找“重要”括号。如果返回false,则可以尝试假定两个字符串相同的回退方法

    找到平衡后的有效括号

    • 查找最右边的索引(使用
      lastIndexOf
    • 计数器=0
    • 对于从该索引向下到4的每个字符(中
      之后的字符:
      • 如果是
        计数器+++
      • 如果是
        计数器--
      • 如果
        计数器==0
        停止,则返回当前索引
    现在你有了有效括号的索引。你的A是4和这个索引-1之间的子字符串(记住
    )前面的空格。你的B是从这个索引+1到你首先找到的右边
    的索引

    回退方法

    假设你的括号不平衡,你能做些什么吗

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

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

  • B表达式中的括号保证正确匹配。即,无
    )((
    :-)
    )等
  • A和B是完全相同的。在这种情况下,即使括号中有不匹配的括号,例如,
    Hello(-:)
    ,您也知道t之前的