我正在使用java创建一个编辑器,其中我的find和replace选项失败,而我用java替换c ++然后在替换我的代码后显示java ++它不会完全取代++文本文件。
所以这里是我使用patcher和matcher在我的代码中创建的查找方法
public void findingValue1() {
p = Pattern.compile(p.quote(tf1.getText()));
m = p.matcher(tavalue);
if (m.find(x)) {
y = 0;
char ch[] = tavalue.toCharArray();
for (int i = 0; i < m.start(); i++)
if (ch[i] == '\n')
y++;
ta.select(m.start() - y, m.end() - y);
r1 = m.start() - y;
r2 = m.end() - y;
x = m.end();
user = true;
} else
cannotFound();
f1.toFront();
}
public void findingValue2() {
try {
s1 = ta.getText();
s1 = s1.replaceAll("\r", "");
ta.setText(s1);
ta.setCaretPosition(pos);
p = Pattern.compile(tf2.getText());
m = p.matcher(s1);
if (m.find(pos)) {
ta.requestFocus();
ta.select(m.start(), m.end());
String str1, str2;
str1 = tf3.getText();
str2 = tf2.getText();
if (str1.length() > str2.length())
pos = m.end() + 1;
else
pos = m.start() + 1;
} else
cannotFound();
} catch (Exception ex) {
cannotFound();
}
}
我知道这不是提问的正确方法,但我的代码非常庞大。 任何人都可以解释为什么会这样吗?
这个 ...
p=Pattern.compile(tf2.getText());
...使用输入到tf2
的文本是一个匹配的正则表达式。 +
字符是在正则表达式中具有特殊含义的几个字符之一。如果要将用户输入视为文字文本,无论其包含哪些字符,您都需要引用它或以其他方式指示。例如,
p = Pattern.compile(Pattern.quote(tf2.getText()));