开发者

Replace the line containing the Regex

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.

My code looks like this,

Pattern p = Pattern.compile("^.*@@.*$");  
String regex = "This is the first l开发者_如何学Cine \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


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)^.*@@.*$".

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.


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.

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)?", "" );


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


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜