import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?s:\n"
+ "<ins\n"
+ "(?:(?!</ins>).)*?\n"
+ "\"adsbygoogle\"\n"
+ ".*?\n"
+ "</ins>)";
final String string = " 0\n"
+ "down vote\n"
+ "favorite\n"
+ " \n\n"
+ "I have a thousand html pages (without admin panel) with codes of adsense. And I want to remove all of them from html. One code looks like:\n\n"
+ "<ins class=\"adsbygoogle\"\n"
+ " style=\"display:inline-block;width:160px;height:600px\"\n"
+ " data-ad-client=\"ca-pub-7165746718333100\"\n"
+ " data-ad-slot=\"9087512399\"></ins>\n\n"
+ "Another:\n\n"
+ "<ins class=\"adsbygoogle\"\n"
+ " style=\"display:inline-block;width:160px;height:600px\"\n"
+ " data-ad-client=\"ca-pub-7163746711373100\"\n"
+ " data-ad-slot=\"7467236139\"></ins>\n\n"
+ "All of them are similar but not equal. A tried to write regex to find and replace it with empty string, but unsuccessfully.\n\n"
+ "Any suggetion how to do it automatically?\n";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html