To let ^ match the beginning of a line and $ match the end of one, you need to enable the multi-line parameter. You can do this by adding (?m) in front of your regular expression as follows: "(?m)^.*@@.*$" .
In addition, you want to keep the grouping until your regular expression finds a match, which can be done as follows:
while(m.find()) { System.out.println("Output: "+m.group()); }
Note that the regex will match these lines (not the ones you specified):
Thus is @@{xyz} should not appear This will not appear @@{abc}
But if you want to replace the lines containing @@ , as the name of your message suggests, do the following:
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: * nix accounts, line breaks on Windows and Mac, as mentioned in PSeed.
source share