我想更改 width="xyz",其中 (xyz) 可以是 width="300"的任何特定值。我研究了正则表达式,这就是我使用正则表达式语法的方法
String holder = "width=\"340\"";
String replacer="width=\"[0-9]*\"";
theWeb.replaceAll(replacer,holder);
其中 theWeb 是字符串 。但这并没有被取代。任何帮助将不胜感激。
最佳答案
您的正则表达式是正确的。您可能会忘记的一件事是,在 Java 中,所有字符串方法都不会影响当前字符串 - 它们仅返回经过适当转换的新字符串。试试这个:
String replacement = 'width="340"';
String regex = 'width="[0-9]*"';
String newWeb = theWeb.replaceAll(regex, replacement); // newWeb holds new text
关于java - 如何使用java更改html文件的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008602/