import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^ *(<[^\\r\\n<>]*>)|(\\s*<[^\\r\\n<>]*>\\s*)(?=[^\\r\\n<]*$)|<\\/?color[^<>]*>";
final String string = "<color=#3D8CBF>using</color> <color=#00FFFF> aaa <Dont remove this tag> System</color>.Collections;\n"
+ "<color=#3D8CBF>using</color> <color=#00FFFF>System</color>.Collections.Generic;\n"
+ "<color=#3D8CBF>using</color> <color=#00FFFF>UnityEngine</color>;\n\n"
+ "<color=#3D8CBF>public</color> <color=#3D8CBF>class</color> ChangePosition : <color=#00FF00>MonoBehaviour</color> {\n\n"
+ " <color=#0000FF>// Update <color=#3D8CBF>is</color> called once per frame\n"
+ "</color>\n"
+ " <color=#3D8CBF>void</color> Update()\n"
+ " {\n"
+ " <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.BeginSample(\"Test 1\");\n"
+ " Test1();\n"
+ " <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.EndSample();\n\n"
+ " <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.BeginSample(\"Test 2\");\n"
+ " Test2();\n"
+ " <color=#00FFFF>UnityEngine</color>.Profiling.Profiler.EndSample();\n"
+ " }\n"
+ "}\n";
final String subst = "\\1\\2";
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