如何使用 RegEx(或可能不使用?)格式化和替换组 (Java)

How to format and replace groups using RegEx (or Maybe not?) (Java)

output = output.replaceAll("%(\w+)%",getVar(""));

public String getVar(name){...return formatted;}

我有这行代码(在 JAVA 中)和一个名为 getVar() 的函数,它将给我具有我们想要的名称的变量值。该功能运行良好,但这段代码似乎不再寻找组。

我用这个正则表达式格式化的字符串是:

"My name is %name% and I am %age% years old."

而不是给我回报:"My name is Paulo and I am 15 years old."(因为name = Pauloage = 15)它没有回报我。不是用 getVar(name)getVar(age) 替换正则表达式,而是用 getVar("").

替换它

是否有某种修复方法,这是错误还是预期的行为?如果是,我怎样才能通过其他方式获得相同的结果?

编辑:

for(String i: varnames){
            output = output.replaceAll("%"+i+"%",getVar(i));
}

这个特定案例的工作...但是,有没有办法在 replaceAll() 中使用函数并维护组(例如 </code>、<code>)在函数内部工作?

编辑 2:

//Variables//
    ArrayList<String> varnames = new ArrayList<String>(0);
    ArrayList<String> varvalues = new ArrayList<String>(0);
//end of Variables

private String getVar(String name){
    String returnv = "";
    if(varnames.contains(name.toLowerCase())) returnv = varvalues.get(varnames.indexOf(name.toLowerCase()));
    //System.out.println("\n\n"+name+"\n\n");
    return returnv;
}

private String format(String input){
    String output = input;

    output = output.replace("[br]","/n");  

    for(String i: varnames){
        output = output.replaceAll("%"+i+"%",getVar(i));//This is how I am parsing the variables.
    }

    //Here I want to handle inline functions... for example: a function called 'invert' that would switch the letters. If the input String that is being formatted (output) contains the regex, the function needs to evaluate and replace.

    //How I tried to do it:
    output.replaceAll("invert\((\w+)\)",invertLetters(""));

        return output;
    }

public String invertLetters(String input){//Inverts the letters of the String}

正如codisfy所提到的,如果您在谈论javajavascript,则根本不清楚,因为您使用replaceAll方法我会考虑你使用 java然而,我将在下文中解释的内容适用于大多数(如果不是全部)正则表达式引擎,与编程语言无关。

当您调用 outputString=inputString.replaceAll("REGEX","REPLACEMENT"); 时,方法 replaceAll 将配置其内部正则表达式引擎,构建一个有限自动机(为简单起见,我们省略 DFA/NFA 之间的区别此处)将正则表达式作为参数传递并分析调用它的 inputString 以进行替换和 return 结果 (outputString)。

当调用该方法时,它还需要替换 String (REPLACEMENT),这是一个可能包含或不包含 back-references 的普通字符串对于某些组,为了启用某些上下文替换(使用语法 </code>、<code></code>、<code>、... 取决于语言)。

理解非常重要的一点是,正则表达式及其替换字符串都只是简单的字符串,在正则表达式引擎之外没有特殊含义(方法replaceAll).

因此,如果您在其他方法中重用它们(例如将它们作为参数传递),它们将被解释为 literally,就像其他普通字符串的情况一样。

所以 而不是 希望 </code> 在您的调用 <code>getVar("") 中被 nameage 替换。它将只是 </code>,没有别的。 </p> <p>另外,话虽这么说,你的方法 <code>public void getVar(name){...} 甚至没有 return 字符串,因为它的 return 类型是 void,因此,如果你在java 并且如果您使用字符串 class 的 replaceAll 方法(需要 2 个字符串作为参数),它将 一开始甚至不会编译

我会让您实现其余代码,但如果您按以下方式更改替换循环,它应该可以工作:

String input="My name is %name% and I am %age% years old.";
Matcher matcher = Pattern.compile("%(\w+)%").matcher(input);
String output=new String(input);
while (matcher.find()) {
          System.out.println("------------------------------------");
          System.out.println("Back reference:" + matcher.group(1));
          String group=matcher.group(1);//give access to the first matching group so that it can be reused
          output=output.replaceAll("%"+group+"%", getVar(group));
       }

System.out.println(output);

getVar方法代码:(我会让你适配)

public static String getVar(String name){ 
    if(name.equals("name"))
        return "Paulo"; 
    return "15"; 
}

输出:

------------------------------------
Back reference:name
------------------------------------
Back reference:age
My name is Paulo and I am 15 years old.