这是一个我无法找到答案的正则表达式问题:
输入:
"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."
期望的输出:
"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>."
解决方案不得涉及首先按句子分割字符串。
我尝试了什么:
一个简单的input.replace(group, replace)
不会起作用,因为已经有一个不应该被替换的匹配。
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;
}
}
答案 0 :(得分:8)
您的代码无法正常工作的原因是您在循环中修改input
,使匹配结果上的索引无效。
但好消息是你根本不需要循环,你可以使用负向lookbehind和负向前导(details here)的组合来跳过已经自动拥有包装器的实例,并使用replaceAll
为您执行循环:
public static void main(String[] args) throws Exception
{
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.";
String result = input.replaceAll("(?<!<start time>)00:00:00(?!<end time>)", "<start time>00:00:00<end time>");
// Negative lookbehind -----------^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
// Negative lookahead ------------------------------------/
System.out.println(result);
}
否定的背后说如果文字在它面前有“#34;不匹配”。并且否定的前瞻说如果文本在此之后就不匹配了。&#34;
答案 1 :(得分:1)
Lookahead and lookbehind断言可以帮助你。
负面反对:"(?<!start)text"
匹配"footext"
但不匹配"starttext"
,
否定前瞻:"text(?!end)"
匹配"textfoo"
但不匹配"textend"
。
将此问题应用于您的案例会导致:"(?<!<start time>)(00:00:00)(?!<end time>)"
。