import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<!--\\s*ID\\:\\s*\\d+\\s*-->(?=\\n*-\\s+)";
final String string = "### Alias\n\n"
+ "Q: what check to do before creating an alias?\n\n"
+ "A: check whether any other command exists by that name using `type new-alias-name`\n"
+ "<!--ID: 1643091253375-->\n\n"
+ "---\n\n"
+ "Q: syntax to create an alias?\n\n"
+ "A: `alias name='string'`\n"
+ "<!--ID: 1645446702965-->\n\n"
+ "- no spaces before or after the equal sign\n"
+ "- single quotes around the command sequence\n"
+ "![[Pasted image 20220125092954.png]]\n"
+ "<!--ID: 1643091253408-->\n\n"
+ "---\n\n"
+ "### Redirection/Piping\n\n"
+ "Q: output of `> ls-output.txt`\n\n"
+ "A: the file will be truncated if it exists or it would be created.\n"
+ "<!--ID: 1645087988730-->\n\n"
+ "- Simply using the redirection operator with no command preceding it will truncate an existing file or create a new empty file\n"
+ "<!--ID: 1643351731943-->\n\n";
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