import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:([A-Z]+ *[A-Z]+)\\n).*?(?=$|([A-Z]+ *[A-Z]+)\\n)";
final String string = " ADAM ASS\n"
+ " Help! Someone help me! (He stops when \n"
+ " he hears a loud dragging sound somewhere \n"
+ " in the room. He looks out into the darkness \n"
+ " and calls out.) Is someone there? Hey! \n"
+ " (He turns back to the corner to which \n"
+ " he is chained, says in a slightly softer \n"
+ " but still panicked voice) Shit, Im \n"
+ " probably dead.\n\n"
+ " Suddenly, from out within the darkness comes a mans low, raspy \n"
+ " voice. It startles Adam. The voice, we will soon learn, belongs \n"
+ " to LAWRENCE.\n\n"
+ " LAWRENCE\n"
+ " Youre not dead.\n\n"
+ " Adam quickly turns in the direction of the voice. Holding his \n"
+ " arms out for balance, he tries to look across the room to whoever \n"
+ " is speaking, but still cannot see a thing.\n\n"
+ " ADAM\n"
+ " Whos that? Whos that?!\n\n"
+ " LAWRENCE\n"
+ " (his voice strangely a bit on the calm \n"
+ " side, and almost irritated with Adams \n"
+ " reaction to the situation. This shall \n"
+ " be his tone for many scenes to come.) \n"
+ " Theres no point in yelling, I already \n"
+ " tried it.\n\n"
+ " ADAM\n"
+ " Turn on the lights!\n\n"
+ " LAWRENCE\n"
+ " Would if I could.\n\n"
+ " ADAM\n"
+ " What the fuck is going on? Where am \n"
+ " I? (He turns into his corner, touching \n"
+ " the wall.)\n\n"
+ " LAWRENCE\n"
+ " I dont know yet.\n\n"
+ " ADAM\n"
+ " (smelling something; in disgust) What \n"
+ " is that smell?\n\n"
+ " LAWRENCE\n"
+ " Shh! Hang on a second, I think I found \n"
+ " something.\n\n"
+ " With a loud click and an even louder buzzing sound, the very \n"
+ " bright fluorescent lights come to life, lighting up in rows, \n"
+ " starting from Lawrences end and moving towards Adam. As they \n"
+ " come on, Adam is nearly blinded by the sudden change from pitch \n"
+ " black to bright white and squints in pain, holding up his arms \n"
+ " to cover his face. In the light we now see that he is in his \n"
+ " mid-twenties, with short brown hair, wearing a dark blue striped \n"
+ " shirt over a white tee shirt and jeans, looking like a drowned \n"
+ " rat from the tub. It takes him a moment but his eyes finally \n"
+ " start to adjust, and he looks around the room. He and we see \n"
+ " Lawrence, who also winces from the glare of the lights, standing \n"
+ " by the light switch and the door. He is on the opposite end of \n"
+ " the room, also chained to a pipe in the corner by his foot. He \n"
+ " wears a blue button-down dress shirt, now soaked with sweat stains. \n"
+ " He is middle aged, mid to late forties, with pale blonde hair \n"
+ " and even paler skin. Dark circles are under his eyes. Both men \n"
+ " are barefoot.\n\n"
+ " Lawrences eyes adjust to the light and he sees across the room. \n"
+ " Then, his gaze starts towards the center of the room, as does \n"
+ " Adams, who steps forward as much as he can, a look of horror \n"
+ " on his face. We see lying face down the body of a man who has \n"
+ " blown his brains out, lying in a pool of blood, clad in only \n"
+ " boxer shorts and a tee shirt. In his left hand is a gun, in his \n"
+ " right hand is a micro cassette recorder. A gunshot and a scream \n"
+ " are heard as the camera moves up and in a fast 360° angle above \n"
+ " and circling the man, ending in a full overhead view of him.\n\n\n"
+ " The shot cuts to Adam, who reels in shock and disgust.\n\n"
+ " ADAM\n"
+ " Holy shit!\n\n"
+ " He turns towards the tub and leans over, gagging and coughing. \n"
+ " Lawrence in the meantime hops forward the best he can, studying \n"
+ " the body with a look of fear and concern. Adam stops coughing \n"
+ " and turns back around, takes another look at the body and around \n"
+ " the room. He looks down at his chain then starts to completely \n"
+ " freak out, grabbing and pulling at his chain.\n\n"
+ " ADAM\n"
+ " (screaming) HELP!!! (He falls back onto \n"
+ " his bottom on the floor as he yanks \n"
+ " at the chain as hard as he can.) HELP!!! \n"
+ " Help!\n\n"
+ " Lawrence just stands and watches him with an almost embarrassed, \n"
+ " appalled look at his behaviour. It seems that Lawrence, despite \n"
+ " being in the situation hes in, is above that kind of uncontrolled \n"
+ " reaction. He speaks a bit coldly.\n\n"
+ " LAWRENCE SANDLER\n"
+ " No one can hear you.";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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