Printing a replaced line in a new text file

纵然是瞬间 提交于 2019-12-02 12:57:28

The replaceAll method on String takes a regular expression as an argument, and in regular expressions some characters have special meanings, such as the parentheses in your expression.

Simply use the replace method instead, which takes literal strings:

String newline = line.replace("stream.Values(strmatch('Test',stream.Components,'exact'))", "New Data");

Don't be confused by the name of the method - the difference between replace and replaceAll is not in how many times they replace, but the difference is in that the first one takes literal strings and the second one takes a regular expression. It's in the Javadoc:

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

public String replace(CharSequence target, CharSequence replacement) {
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!