import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^.*(?=(?:[^\\sA-Z]*[A-Z]){2})";
final String string = "Unseen ServanTThis creature can be summoned with the spell Unseen Servant.Unseen ServantCreature -1No AlignmentMediumMindlessSource Core Rulebook pg. 380Perception +0; darkvisionLanguages - (understands its creator)Skills Stealth +8Str -4, Dex +2, Con +0, Int -5, Wis +0, Cha +0Invisible An unseen servant is invisible, though it normally doesn’t Sneak, so it is usually only hidden.AC 13, Fort +0, Ref +4, Will +0HP 4; Immunities disease, mental, non-magical attacks, paralysis, poison, precision, unconscious\n"
+ "; Resistances all damage 5 (except force or ghost touch)Speed fly 30 feetForce Body An unseen servant’s physical body is made of force. It can’t use attack actions. It can move and use Interact actions to do things such as fetch objects, open unstuck or unlocked doors, hold chairs, and clean. It can’t pass through solid objects.\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
if (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