import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=\\,|\\+|\\~|\\>|\\(|\\)|\\[|\\]|\\{|\\}|^)(?<s>\\s*)(?<tag>a|div|p|span|img|button)(?!\\s*\\[[^\\]]*__canvas[^\\]]*\\])(?=[\\:\\,\\+\\~\\>\\(\\)\\[\\]\\{\\}\\s]+)(?=[^\\}\\;]*[\\{])";
final String string = "@media only screen and (max-width: 600px) {\n"
+ " div>a[__canvas]:hover,a,div {\n"
+ " background-color: lightblue;\n"
+ " }\n\n"
+ " i+img>audio,table:not(a+p),\n"
+ " button~.a \n"
+ " , \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " span{\n"
+ " background-color: lightblue;\n"
+ " }\n"
+ "}\n\n"
+ "a {\n"
+ " background: url(http://a.b.com);\n"
+ " font-family: cursive, a, serif;\n"
+ "}\n\n"
+ "img\n"
+ " \n"
+ " \n"
+ " {\n"
+ " color: a;\n"
+ " font-family: a, serif;\n"
+ "}";
final String subst = "${s}${tag}[__canvas]";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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