这是我的代码,我看过那个表明它可以接受String的库。不知道这里有什么问题。
String currentText = "";
String regex = "";
public void prepRegex() {
currentText = textField.getText();
regex = regexField.getText();
}
public void checkFunc() {
prepRegex();
try {
Pattern regex = Pattern.compile(regex); // This doesn't work <---
Matcher regexMatcher = regex.matcher(currentText);
while (regexMatcher.find()) {
for (int i = 1; i <= regexMatcher.groupCount(); i++) {
// matched text: regexMatcher.group(i)
// match start: regexMatcher.start(i)
// match end: regexMatcher.end(i)
}
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
}
答案 0 :(得分:3)
您在自己的初始化程序中引用变量:
Pattern regex = Pattern.compile(regex);
^ This ^ is the same symbol as this
如果您想引用实例变量(上面的String regex = "";
),请使用this
对其进行限定:
Pattern regex = Pattern.compile(this.regex);