import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<=\\s)((?:docstring|some_detailed_notes)\\s*=\\s*\\(?\\s*(?://.*\\n\\s*)*)\"((?:[^\"\\\\]|\\\\.)+)\"(\\s*\\)?\\s*;)";
final String string = "class myClassA(myBaseClass) {\n\n"
+ " docstring = // End-of-line-comments possible\n"
+ "\"\n"
+ "This is my class description docstring stored in a string variable inherited from\n"
+ "myBaseClass.\n"
+ "The content of this string, INCLUDING INDENTATION, MUST NOT be changed.\n"
+ "\";\n\n"
+ "docstring = (\n"
+ "\"\n"
+ "Enclosed in parentheses\n"
+ "\"\n"
+ ");\n\n"
+ " // This variable is declared as string has a mismatched indentation:\n"
+ "string some_detailed_notes = \"Some string like this is also possible.\n"
+ " Also with\n"
+ " some really\n"
+ " strange indentation\n"
+ " and `inline code`, ```code blocks``` $\\\\text{LaTeX}$ $$\\\\frac{1}{2}$$\n"
+ ":::{hint}\n"
+ "some admonitions\n"
+ ":::\n"
+ "and all kind of special characters such as:\n"
+ "\\\"\n"
+ "(...)\n"
+ "{...}\n"
+ "[...]\n"
+ ";.,\n"
+ "etc.\n\n"
+ "which MUST be preserved.\";\n\n"
+ " string some_other_string = \"\n"
+ " do NOT touch this, only docstring and some_detailed_notes\n"
+ " \";\n"
+ "}";
final String subst = "\\1R\"\"\"\"\\2\"\"\"\"\\3";
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