Given:
-- input --
Keep this.
And keep this.
And keep this too.
Chomp this chomp:
Anything beyond here gets chomped.
-- output (expected) --
Keep this.
And keep this.
And keep this too.
How can I match a regex per a grouping so that once "chomp:" is found, everything from the beginning of that line as well as after gets chomped (deleted)?
String text = "Keep this.\nAnd keep this.\n\nAnd keep this too.\n"
+ "This could be anything here chomp:\nAnything beyond here gets chomped.";
Pattern CHOMP= Pattern.compile("^((.*)chomp:(.*))$", Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = CHOMP.matcher(text);
if (m.find()) {
int count = m.groupCount();
//
// How can I match a group here to either delete or keep for expected output?
//
// text = <match a group to assign or replace non-desired text>;
System.out.println(text); // Should output contents from above -- output (expected) --
}