import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "[-*] ?([^-*].*?) ?[-*]";
final String string = "The Zen of Python, by Tim Peters\n\n"
+ "Beautiful is better than ugly.\n"
+ "Explicit is better than implicit.\n"
+ "Simple is better than complex.\n"
+ "Complex is better than complicated.\n"
+ "Flat is better than nested.\n"
+ "Sparse is better than dense.\n"
+ "Readability counts.\n"
+ "Special cases aren't special enough to break the rules.\n"
+ "Although practicality beats purity.\n"
+ "Errors should never pass silently.\n"
+ "Unless explicitly silenced.\n"
+ "In the face of ambiguity, refuse the temptation to guess.\n"
+ "There should be one-- and preferably only one --obvious way to do it.\n"
+ "Although that way may not be obvious at first unless you're Dutch.\n"
+ "Now is better than never.\n"
+ "Although never is often better than *right* now.\n"
+ "If the implementation is hard to explain, it's a bad idea.\n"
+ "If the implementation is easy to explain, it may be a good idea.\n"
+ "Namespaces are one honking great idea -- let's do more of those!";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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