删除正则表达式组匹配中的双空行

时间:2015-05-04 11:59:27

标签: java regex

情况:我有一些文字,只能使用一个正则表达组来达到目标​​。我需要在超过5" ="之后剪切文本。并删除双空行。

这是匹配文本的正则表达式。编程语言是Java。 它匹配5行或更多" ="

的新行之前的所有内容
([^]+?)\n[=]{5,}

现在我需要替换匹配组中的所有双空行。我无法更改Java代码,我唯一能改变的是结果中的匹配组和正则表达式本身。

示例文字:

  

Hello World

     

这是文字。

     


     

干杯

     

================

     

不重要的文字

应该导致:

  

Hello World

     

这是文字。

     

干杯

Java代码如下,但无法更改:

String regex = "([\\s|\\S]+?)\n[=]{5,}";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
    for (int i = 0; i < matcher.groupCount(); i++) {
         System.out.println("Group " + i + ":\n" + matcher.group(i));
    }
}

只能更改正则表达式

2 个答案:

答案 0 :(得分:0)

try {
    String resultString = YOURSTRING.replaceAll("(?ism)[=]{5,}.*", "");
    resultString = resultString.replaceAll("(?ism)^\\s+$", "");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
} catch (IllegalArgumentException ex) {
    // Syntax error in the replacement text (unescaped $ signs?)
} catch (IndexOutOfBoundsException ex) {
    // Non-existent backreference used the replacement text
}

第一个正则表达式替换[=]{5,}(5个或更多=),以及之后的所有文本 第二个将清除空白行。

答案 1 :(得分:0)

我不相信正则表达式能够在一次通过中智能地执行此操作(2次传递是蛋糕)。

但是,我设计了一些有点丑陋的东西......标准的重复量词不会这样做,因为你想要修改子内容而你无法访问底层的java。

(?:([\s\S]*?)(?:(\n\n)\n\n)?)(?:([\s\S]*?)(\n\n)\n\n)?([\s\S]*?)={5,}[\s\S]*

它将前四个“空白行”前的所有内容捕获为$ 1,它将前两个换行符捕获为$ 2,以便稍后替换。

下一组是相同的,除了它后跟?量词意味着0或1次,因此是可选的。该组将内容记录为3美元,新行记录为4美元。

最后,最后一组最后是满意的,5美元。

您可以根据需要多次重复此群组。

这是一个版本,按照相同的模式重复四次,组$ 1,$ 3,$ 5,$ 7,$ 9包含过多换行符之间的内容,$ 2,$ 4,$ 6,$ 8,$ 10包含两个换行符,$ 11包含内容。

(?:([\s\S]*?)(?:(\n\n)\n\n)?)(?:([\s\S]*?)(\n\n)\n\n)?(?:([\s\S]*?)(\n\n)\n\n)?(?:([\s\S]*?)(\n\n)\n\n)?(?:([\s\S]*?)(\n\n)\n\n)?([\s\S]*?)={5,}[\s\S]*

在上面使用正则表达式的情况下。您的替换看起来像$1$2$3$4$5$6$7$8$9$10$11

当然,它并不漂亮,但它与您所拥有的相结合。

最后,对第一个正则表达式的解释(因为第二个正则表达式更重复。

 (?:                      # Opens NCG1
     (                    # Opens CG1
         [\s\S]*?         # Character class (any of the characters within)
                            # A character class and negated character class, common expression meaning any character.
                            # * repeats zero or more times
                            # ? as few times as possible
     )                    # Closes CG1
     (?:                  # Opens NCG2
         (                # Opens CG2
             \n           # Token: \n (newline)
             \n           # Token: \n (newline)
         )                # Closes CG2
         \n               # Token: \n (newline)
         \n               # Token: \n (newline)
     )?                   # Closes NCG2
                            # ? repeats zero or one times
 )                        # Closes NCG1
 # begin repeat section
 (?:                      # Opens NCG3
     (                    # Opens CG3
         [\s\S]*?         # Character class (any of the characters within)
                            # A character class and negated character class, common expression meaning any character.
     )                    # Closes CG3
     (                    # Opens CG4
         \n               # Token: \n (newline)
         \n               # Token: \n (newline)
     )                    # Closes CG4
     \n                   # Token: \n (newline)
     \n                   # Token: \n (newline)
 )?                       # Closes NCG3
 # end repeat section
 (                        # Opens CG5
     [\s\S]*?             # Character class (any of the characters within)
 )                        # Closes CG5
 ={5,}                    # Literal =
                            # Repeats 5 or more times
 [\s\S]*                  # Character class (any of the characters within)
                            # * repeats zero or more times