Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java使用正则表达式替换特定的子字符串_Java_Regex - Fatal编程技术网

如何使用java使用正则表达式替换特定的子字符串

如何使用java使用正则表达式替换特定的子字符串,java,regex,Java,Regex,我有一个字符串如下 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 
   $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");
我想用贪婪匹配的正则表达式替换字符串。
请为我提供一些实现同样目标的指针。

简易解决方案不要使用正则表达式,而是使用字符串方法包含。 制作一个扫描器对象,逐个行解析字符串,并将结果添加到字符串缓冲区

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);
}

使用正则表达式和测试类的新答案

import java.util.Scanner;


public class RegEx {

public static void main(String[] args) {
    String text = "when\n$Alarm:com.abc.Alarm(description == \"abc\")\nthen\nlogger.info(\"alarm description\")";
    System.out.println(text);
    StringBuilder sb = new StringBuilder();
    Scanner scan = new Scanner(text);
    while(scan.hasNextLine()){
        String line = scan.nextLine();
        if(line.matches(".*\\.Alarm(.*).*")){
            line+=" from entry-point \"AlarmStream\"";
        }
        sb.append(line+System.getProperty("line.separator"));
    }
    System.out.println(); // Nicer output
    System.out.println(sb.toString());
}
}

输出

什么时候

$Alarm:com.abc.Alarm(描述==“abc”)

然后

logger.info(“报警描述”)

什么时候

$Alarm:com.abc.Alarm(description==“abc”)来自入口点“AlarmStream”

然后

logger.info(“报警描述”)


谢谢您的更新。但我的问题是,第一个案例中的“.Event”(“and”)”和“.Alarm”(“and”)”将保持不变,剩下的部分将在when部分中更改,因此我不想进行精确的字符串比较。@gsr我为您提供了一个新的答案,请检查它是否满足您的要求。