tags:

views:

115

answers:

4

Hi,
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 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  A: 

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.

Bart Kiers
Thanks Bart, But is there a way to get the string, without the lines that matched the pattern.(I need that.)
Amit
Yes, I showed how to do that in my `replaceAll(...)` example.
Bart Kiers
A: 

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

Priyank Bolia
+1  A: 

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)?", "" );
PSpeed
As another poster points out, adding \n? to the end will replace without leaving blank lines... though should be expanded to \r?\n? if you think you might get DOS-style line endings.
PSpeed
Thanks, PSpeed, Can you please give the regex, that returns the string without leaving the blank lines.
Amit
Full regex has been added.
PSpeed
A: 

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.

Neal Swearer