我想静态地创建一个 RegEx 模式,但我认为我的语法有误?
static {
Pattern noHREF = Pattern.compile("<a.+?>", Pattern.CASE_INSENSITIVE);
}
public static String getStringWithHREFsRemoved(String html) {
Matcher m = noHREF.matcher(html);
etc.....
最佳答案
您需要将noHREF
变量作为类的静态成员变量。
static Pattern noHREF = Pattern.compile("<a.+?>", Pattern.CASE_INSENSITIVE);
public static String getStringWithHREFsRemoved(String html) {
Matcher m = noHREF.matcher(html);
// ...
在您在问题中编写的代码中,noHREF 变量是一个局部(临时)变量,其范围在 static {
和 }
之间。
关于Java静态实例创建者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3478313/