import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^step\\s*(\\d+)\\s*(.*?)\\s*(?=^step\\s*\\d|\\Z)";
final String string = "This is the first line of the file.\n"
+ "here we tell you to make a tea.\n\n"
+ "step 1\n\n"
+ "Pour more than enough water for a cup of tea into a regular pot, and bring it to a boil.\n\n"
+ "step \n"
+ "2\n\n"
+ " This will prevent the steeping water from dropping in temperature as soon as it is poured in.\n\n"
+ "step 3 \n\n\n"
+ "When using tea bags, the measuring has already been done for you - generally it's one tea bag per cup.";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | 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