Java编辑器软件

时间:2019-04-04 12:04:28

标签: java awt

我正在使用Java创建一个编辑器,当我用Java替换c ++时,我的“查找和替换”选项失败,然后在替换我的代码后显示java ++,它不能完全替换++到文本文件中。

这是我使用修补程序和匹配器在代码中创建的查找方法

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();
    }

}

我知道这不是问问题的正确方法,但是我的代码非常庞大。
谁能解释为什么会这样?

1 个答案:

答案 0 :(得分:1)

这个...

        p=Pattern.compile(tf2.getText());

...使用输入到tf2中的文本为正则表达式进行匹配。 +字符是正则表达式中具有特殊含义的几个字符之一。如果要将用户输入视为文字文本,而不管其包含什么字符,则需要将其引用或以其他方式指明。例如,

        p = Pattern.compile(Pattern.quote(tf2.getText()));
相关问题