我有一个字符串如下
when
$Event:com.abc.Event(description == "abc")
then
logger.info("description");
我需要将上面的字符串替换为下面的
when
$Event:com.abc.Event(description == "abc") from entry-point "EventStream"
then
logger.info("description");
遇到的时候也是这样
when
$Alarm:com.abc.Alarm(description == "abc")
then
logger.info("alarm description");
我需要进行如下更改
when
$Alarm:com.abc.Alarm(description == "abc") from entry-point "AlarmStream"
then
logger.info("alarm description");
我想使用贪婪匹配的正则表达式替换字符串。 请为我提供一些指导以实现同样的目标。
最佳答案
简单的解决方案不用担心正则表达式,而是使用 Strings 方法包含。 创建一个 Scanner 对象,逐行解析字符串并将结果添加到字符串缓冲区。
if(line.contains("$Event:com.abc.Event(description == "abc")"){
sb.append(line + "from entry-point \"EventStream\" ");
} else if(line.contains("$Alarm:com.abc.Alarm(description == \"abc\")") {
sb.append(line + "from entry-point \"AlarmStream\" ");
}else {
sb.append(line);
}
关于java - 如何使用java使用正则表达式替换特定子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7191219/