替换包含正则表达式的行

时间:2020-12-15 19:15:18

I have an input string containing multiple lines(demarcated by \n). I need to search for a pattern in the lines and if its found, then replace the complete line with empty string.

我有一个包含多行的输入字符串(由\ n划分)。我需要在行中搜索一个模式,如果找到它,则用空字符串替换整行。

My code looks like this,

我的代码看起来像这样,

Pattern p = Pattern.compile("^.*@@.*$");  
String regex = "This is the first line \n" +  
               "And this is second line\n" +  
               "Thus is @@{xyz} should not appear \n" +  
               "This is 3rd line and should come\n" +  
               "This will not appear @@{abc}\n" +  
               "But this will appear\n";  
Matcher m = p.matcher(regex);  
System.out.println("Output: "+m.group());  

I expect the response as :

我希望回复为:

Output: This is the first line       
        And this is second line  
        This is 3rd line and should come  
        But this will appear.

I am unable to get it, please help, me out.

我无法得到它,请帮帮我。

Thanks,
Amit

4 个解决方案

#1


3  

Others mention turning on multiline mode but since Java does not default to DOTALL (single line mode) there is an easier way... just leave the ^ and $ off.

其他人提到打开多线模式,但由于Java没有默认为DOTALL(单线模式),因此有一种更简单的方法......只需离开^和$即可。

String result = regex.replaceAll( ".*@@.*", "" );

Note that the issue with either this or using:

请注意以下问题或使用:

"(?m)^.*@@.*$" 

...is that it will leave the blank lines in. If it is a requirement to not have them then the regex will be different.

...是否会留下空白行。如果要求不具备它们,那么正则表达式将是不同的。

Full regex that does not leave blank lines:

完全正则表达式不留空行:

String result = regex.replaceAll( ".*@@.*(\r?\n|\r)?", "" );

#2


5  

In order to let the ^ match the start of a line and $ match the end of one, you need to enable the multi-line option. You can do that by adding (?m) in front of your regex like this: "(?m)^.*@@.*$".

为了让^匹配一行的开头而$匹配一行的结尾,您需要启用多行选项。你可以通过在你的正则表达式前加上(?m)来做到这一点:“(?m)^。* @@。* $”。

Also, you want to keep grouping while your regex finds a match, which can be done like this:

此外,您希望在正则表达式找到匹配项时保持分组,可以这样做:

while(m.find()) {
  System.out.println("Output: "+m.group());
}

Note the regex will match these lines (not the ones you indicated):

请注意,正则表达式将匹配这些行(而不是您指定的行):

Thus is @@{xyz} should not appear 
This will not appear @@{abc}

But if you want to replace the lines that contain @@, as the title of your post suggests, do it like this:

但是如果你想替换包含@@的行,就像你帖子的标题所暗示的那样,那样做:

public class Main { 
    public static void main(String[] args) {
        String text = "This is the first line \n" +  
                      "And this is second line\n" +  
                      "Thus is @@{xyz} should not appear \n" +  
                      "This is 3rd line and should come\n" +  
                      "This will not appear @@{abc}\n" +  
                      "But this will appear\n";  
        System.out.println(text.replaceAll("(?m)^.*@@.*$(\r?\n|\r)?", ""));
    }
}

Edit: accounted for *nix, Windows and Mac line breaks as mentioned by PSeed.

编辑:占PSeed提到的* nix,Windows和Mac换行符。

#3


-1  

Is there a multiline option in Java, check the docs. There is one in C# atleast, I think that should be the issue.

Java中是否有多行选项,请检查文档。在C#至少有一个,我认为这应该是问题。

#4


-1  

Take a look at the JavaDoc on the Matcher.matches() method:

看看Matcher.matches()方法上的JavaDoc:

boolean java.util.regex.Matcher.matches()
Attempts to match the entire input sequence against the pattern. 

If the match succeeds then more information can be obtained via the start, end, and group methods. 

Returns:
true if, and only if, the entire input sequence matches this matcher's pattern

Try calling the "matches" method first. This won't actually do the text replacement as noted in your post, but it will get you further.

首先尝试调用“匹配”方法。这实际上不会像您的帖子中所述那样进行文本替换,但它会让您更进一步。

#1


3  

Others mention turning on multiline mode but since Java does not default to DOTALL (single line mode) there is an easier way... just leave the ^ and $ off.

其他人提到打开多线模式,但由于Java没有默认为DOTALL(单线模式),因此有一种更简单的方法......只需离开^和$即可。

String result = regex.replaceAll( ".*@@.*", "" );

Note that the issue with either this or using:

请注意以下问题或使用:

"(?m)^.*@@.*$" 

...is that it will leave the blank lines in. If it is a requirement to not have them then the regex will be different.

...是否会留下空白行。如果要求不具备它们,那么正则表达式将是不同的。

Full regex that does not leave blank lines:

完全正则表达式不留空行:

String result = regex.replaceAll( ".*@@.*(\r?\n|\r)?", "" );

#2


5  

In order to let the ^ match the start of a line and $ match the end of one, you need to enable the multi-line option. You can do that by adding (?m) in front of your regex like this: "(?m)^.*@@.*$".

为了让^匹配一行的开头而$匹配一行的结尾,您需要启用多行选项。你可以通过在你的正则表达式前加上(?m)来做到这一点:“(?m)^。* @@。* $”。

Also, you want to keep grouping while your regex finds a match, which can be done like this:

此外,您希望在正则表达式找到匹配项时保持分组,可以这样做:

while(m.find()) {
  System.out.println("Output: "+m.group());
}

Note the regex will match these lines (not the ones you indicated):

请注意,正则表达式将匹配这些行(而不是您指定的行):

Thus is @@{xyz} should not appear 
This will not appear @@{abc}

But if you want to replace the lines that contain @@, as the title of your post suggests, do it like this:

但是如果你想替换包含@@的行,就像你帖子的标题所暗示的那样,那样做:

public class Main { 
    public static void main(String[] args) {
        String text = "This is the first line \n" +  
                      "And this is second line\n" +  
                      "Thus is @@{xyz} should not appear \n" +  
                      "This is 3rd line and should come\n" +  
                      "This will not appear @@{abc}\n" +  
                      "But this will appear\n";  
        System.out.println(text.replaceAll("(?m)^.*@@.*$(\r?\n|\r)?", ""));
    }
}

Edit: accounted for *nix, Windows and Mac line breaks as mentioned by PSeed.

编辑:占PSeed提到的* nix,Windows和Mac换行符。

#3


-1  

Is there a multiline option in Java, check the docs. There is one in C# atleast, I think that should be the issue.

Java中是否有多行选项,请检查文档。在C#至少有一个,我认为这应该是问题。

#4


-1  

Take a look at the JavaDoc on the Matcher.matches() method:

看看Matcher.matches()方法上的JavaDoc:

boolean java.util.regex.Matcher.matches()
Attempts to match the entire input sequence against the pattern. 

If the match succeeds then more information can be obtained via the start, end, and group methods. 

Returns:
true if, and only if, the entire input sequence matches this matcher's pattern

Try calling the "matches" method first. This won't actually do the text replacement as noted in your post, but it will get you further.

首先尝试调用“匹配”方法。这实际上不会像您的帖子中所述那样进行文本替换,但它会让您更进一步。