我有一个代码,查找正则表达式匹配,并在正则表达式匹配后添加一个新行。这可以在第一次点击按钮时起作用,但是当我再次单击它时,它仍然会捕获正则表达式。Java正则表达式匹配:负面看起来不工作
示例场景:
变化与结尾的所有句子 '的狗。'并添加一个新行,如果它不是该行的结尾。鼠标点击事件里面
示例代码:
Pattern pattern = Pattern.compile("dog\\.(?!\n)");
Matcher matcher = pattern.matcher(paragraph);
paragraph = matcher.replaceAll("dog\\.\n");
样品输入:
我发现了一个街头的动物。这是一只狗。然后,狗找到另一只狗。看到两只狗互相吠叫,真是有趣。
按钮,首先点击实现代码:
I found a street animal. It was a dog.
Then the dog found another dog.
It was kind of fun to see the two dogs barking to each other.
按钮的第二次点击实现代码:
I found a street animal. It was a dog.
Then the dog found another dog.
It was kind of fun to see the two dogs barking to each other.
我为什么在第二次点击,就没有注意到很困惑没有更多的“狗”。没有新的路线。
在此先感谢您的帮助!
使用Pattern Pattern = Pattern.compile(“dog \\。(?!\\ n)”);' – anubhava
嗨@anubhava,我试过了,但结果仍然一样。 –
也许'(?!\\ r \\ n)'工作,如果文件使用CRLF –