import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(PANEL\\s+\\S+)(.*?)(?=PANEL|\\Z)";
final String string = "PANEL NORMAL\n"
+ "Diana and Gahriel run away with all their might. The monster is raging right behind them\n\n"
+ " Gharial (whisper)\n"
+ " Don’t turn back!\n\n"
+ " Diana (confused)\n"
+ " What--\n"
+ " \n"
+ "PANEL LEFT\n\n"
+ "Gahriel signal diane to be quiet\n\n"
+ " Gharial (put his hand to his mouth signal quiet)\n"
+ " Shh….\n\n"
+ "PANEL FOCUS\n\n"
+ "The monster is running toward them quickly , right behind them\n\n"
+ "PANEL FULL\n\n"
+ "Diana is afraid for her own life. Her eyes wide open as the monster breathes through her.\n\n"
+ " Diana (holding her mouth with her hand)\n"
+ " Gasp\n\n"
+ "PANEL RIGHT\n"
+ " Gahriel (whisper)\n"
+ " Everything will be okay!\n\n"
+ "PANEL CENTER\n"
+ "They both stand quietly, not a single noise.,\n\n"
+ "PANEL SMALL\n"
+ "A sudden noise\n"
+ "Crack\n\n"
+ "PANEL RIGHT\n"
+ "The monster caught Diana and bit her head off. Gahriel in shock-terror, he can’t move.\n\n";
final String subst = "<div class=\"$1\">$2</div>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
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