Replace string containing regex

I have an input line containing several lines (unmanned by \ n). I need to find a pattern in strings and, if found, then replace the complete string with an 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 an answer like:

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

I can not get it, please help me.

Thanks,
Amit

+4
source share
4 answers

Others talk about enabling multi-line mode, but since Java does not default to DOTALL (single-line mode), there is an easier way ... just leave ^ and $ off.

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

Please note that the problem with this or using:

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

... is that it will leave blank lines. If this requirement is not to have them, then the regular expression will be different.

A full regex that does not leave blank lines:

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

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.

+5
source

There is a multi-line parameter in Java, check the docs. There is one in C #, in my opinion, this should be a problem.

-one
source

Take a look at JavaDoc's 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 pattern 

First try calling the matches method. This will not actually replace the text as indicated in your post, but it will help you.

-one
source

All Articles