import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^[\\ \\t]*(\\S?(?:.*\\S)?)?[\\ \\t]*$";
final String string = "The door swung open to reveal pink giraffes and red elephants. \n"
+ " He stomped on his fruit loops and thus became a cereal killer.\n"
+ " Lightning Paradise was the local hangout joint where the .\n"
+ " \n"
+ " \n"
+ "a\n"
+ "sd\n"
+ "a \n\n"
+ "He didn’t want to go to the dentist, yet he went anyway. \n"
+ "He decided to count all the sand on the beach as a hobby. \n"
+ " \n\n"
+ "* Situps are a terrible way to end your day. \n"
+ "The random sentence generator generated a random sentence about a random sentence.\n"
+ "As time wore on, . \n"
+ " The beach was crowded with snow leopards. \n"
+ " \n"
+ "She was too short to see over the fence.\n\n"
+ " \n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
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