如何使用java使用正则表达式替换特定的子字符串?
我有一个字符串如下如何使用java使用正则表达式替换特定的子字符串?,java,Java,我有一个字符串如下 $Trap:com.oss.Event(description matches "abc") stmt="$Trap:com.oss.Event(description matches "abc") or $Trap:com.oss.Event(description matches "cde"); 在上面的字符串中,通用部分是$Trap:com.oss.Event() 当我遇到上面的字符串时,我需要替换为下面的字符串 $Trap:com.oss.Event(de
$Trap:com.oss.Event(description matches "abc")
stmt="$Trap:com.oss.Event(description matches "abc") or $Trap:com.oss.Event(description matches "cde");
在上面的字符串中,通用部分是$Trap:com.oss.Event()
当我遇到上面的字符串时,我需要替换为下面的字符串
$Trap:com.oss.Event(description matches "abc") from entry-point "EventStream".
为了实现上述目标,我在java中使用了以下逻辑
String stmt= $Trap:com.oss.Event(description matches "abc")
if(stmt.matches(".*\\.Event(.*).*")&& s.equals(""))
{
stmt+=" from entry-point "+"ABCStream";
}
但是,当字符串如下所示时,上述逻辑无法按预期工作
$Trap:com.oss.Event(description matches "abc")
stmt="$Trap:com.oss.Event(description matches "abc") or $Trap:com.oss.Event(description matches "cde");
我需要使用正则表达式生成以下对应的字符串
$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches "cde") from entry-point "ABCStream"
请提供一些指针来实现相同的功能。以下Java代码可以工作:
String stmt = "$Trap:com.oss.Event(description matches \"abc\")"
+ " or $Trap:com.oss.Event(description matches \"cde\")";
Pattern p = Pattern.compile(".*?\\.Event\\(.*?\\)");
String res = p.matcher(stmt).replaceAll("$0 from entry-point \"ABCStream\"");
System.err.println(res);
并产生以下结果:
$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches "cde") from entry-point "ABCStream"
您需要在regexp中引用特殊字符,例如圆括号,还需要使用惰性匹配,例如*?。$0引用找到的匹配组
如果您重复执行此替换,则使用
Pattern.compile()
将节省一些性能。以下Java代码有效:
String stmt = "$Trap:com.oss.Event(description matches \"abc\")"
+ " or $Trap:com.oss.Event(description matches \"cde\")";
Pattern p = Pattern.compile(".*?\\.Event\\(.*?\\)");
String res = p.matcher(stmt).replaceAll("$0 from entry-point \"ABCStream\"");
System.err.println(res);
并产生以下结果:
$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches "cde") from entry-point "ABCStream"
您需要在regexp中引用特殊字符,例如圆括号,还需要使用惰性匹配,例如*?。$0引用找到的匹配组
如果您重复执行此替换,则使用Pattern.compile()
将为您节省一些性能。这样做:
public class A {
public static void main(String[] args) {
String stmt="$Trap:com.oss.Event(description matches \"abc\") or $Trap:com.oss.Event(description matches \"cde\")";
System.out.println(stmt);
System.out.println(stmt.replaceAll("(\\$Trap:com.oss.Event\\([^)]*\\))", "$1 from entry-point \"ABCStream\""));
}
}
如您所见,您必须对某些符号进行双重转义。第一个和最后一个括号用于对正则表达式进行分组,您可以使用“$1”打印该组
并处理了这个输出:
$Trap:com.oss.Event(description matches "abc") or $Trap:com.oss.Event(description matches "cde")
$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches "cde") from entry-point "ABCStream"
这就是我们要做的:
public class A {
public static void main(String[] args) {
String stmt="$Trap:com.oss.Event(description matches \"abc\") or $Trap:com.oss.Event(description matches \"cde\")";
System.out.println(stmt);
System.out.println(stmt.replaceAll("(\\$Trap:com.oss.Event\\([^)]*\\))", "$1 from entry-point \"ABCStream\""));
}
}
如您所见,您必须对某些符号进行双重转义。第一个和最后一个括号用于对正则表达式进行分组,您可以使用“$1”打印该组
并处理了这个输出:
$Trap:com.oss.Event(description matches "abc") or $Trap:com.oss.Event(description matches "cde")
$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches "cde") from entry-point "ABCStream"
你不能只用String.Replace吗?你不能只用String.Replace吗?