This a regex question for which I couldn't find an answer yet:
Input:
"the current time is <start time>00:00:00<end time>. at 00:00:00 there is a firework. Another appearance of 00:00:00."
Desired output:
"the current time is <start time>00:00:00<end time>. at <start time>00:00:00<end time> there is a firework. Another appearance of <start time>00:00:00<end time>."
The solution must not involve first splitting the string by sentence.
What I tried:
A simple input.replace(group, replace)
won't work because there is already a match that shouldn't be replaced.
public static void main(String[] args) throws ParseException
{
String input = "the current time is <start time>00:00:00<end time>. at 00:00:00 there is a firework. Another appearance of 00:00:00.";
Pattern p = Pattern.compile("(<start time>)?(00:00:00)(<end time>)?");
Matcher m = p.matcher(input);
while(m.find())
{
if(m.group(1) != null) { continue; }
String substr1 = input.substring(0, m.start(2));
String substr2 = input.substring(m.end(2), input.length());
String repl = "<start time>" + m.group(2) + "<end time>";
input = substr1 + repl + substr2;
}
}