我希望用Java中的正则表达式替换一些正则表达式,例如:
但输出结果为 (xyz)+P。
要求:
输入:xyxyxyP
所需输出:xyzxyzxyzP
这意味着我想将“(for)+\{”替换为“(for\{)+\{”。有没有办法做到这一点?
我尝试了以下代码
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class ReplaceDemo2 {
private static String REGEX = "(xy)+P";
private static String INPUT = "xyxyxyP";
private static String REGEXREPLACE = "(xyz)+P";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REGEXREPLACE);
System.out.println(INPUT);
}
}
但输出结果为 (xyz)+P。
.replaceAll("xy|(?!P)[^x]*(?:x(?!y)[^x]*)*(?=.*P)", "$0z")
。 - undefined