需要在字符串中找到属性名

need to find the attribute name in the string

我正在尝试捕获 Java 行中的最后一个属性。请调整以下代码并帮助我找到 "battr"。例如,我将属性命名为 "attr" 和 "battr",但它可以是任何东西。

System.out.println("<tag attr=\"val1\" battr=\"val2\"></tag>".replaceAll(".*([a-z]+\s*=).*",""));

你可以试试这个:

System.out.println("<tag attr=\"val1\" battr=\"val2\"></tag>".replaceAll(".*(\s([a-z])+\s*=).*", "")
            .replace("=", "").trim());

编辑:

您可以为您的要求使用正向预测:

System.out.println("<tag attr=\"val1\" battr=\"val2\"></tag>".replaceAll(".*((\s|^)([a-z])+(?==)).*", "").trim());

更多信息:Lookahead and Lookbehind Zero-Width Assertions