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]+?|$)";
final String string = "var массив = [\n\n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n\n"
+ "function(){\n\n"
+ "var слой = document.createElement(\"div\")\n"
+ "слой.id = \"ID\"\n"
+ "document.body.appendChild(слой)\n"
+ "слой.style.width = \"200px\"\n"
+ "слой.style.height = \"200px\"\n"
+ "слой.style.position = \"absolute\"\n"
+ "слой.style.left = \"0%\"\n"
+ "слой.style.top = \"0%\"\n"
+ "слой.style.filter = \"drop-shadow(1px 1px 5px Yellow)\"\n"
+ "слой.style.boxShadow = \"1px 1px 5px IndianRed\"\n\n\n"
+ "return 1000},\n\n"
+ "function(){\n"
+ "элемент = ID\n"
+ "элемент.style.transition = \"all 1s cubic-bezier(0.5,-0.5,0.5,1.5) \"\n"
+ "элемент.style.position = \"absolute\"\n"
+ "элемент.style.left = \"33%\"\n"
+ "элемент.style.top = \"33%\"\n\n"
+ "return 1000},\n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n\n\n"
+ "]";
final String subst = "$1";
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