如何用某个词替换最后一个词文本区域?

how replace last word text area with some word?

我从 madprogrammer 那里得到了代码:

但我需要替换文本区域中的最后一个词,可能使用文档过滤或通过 space span(" ") between lastword 和 beforelastword。

有人可以帮我吗?我在google里搜索还是没找到

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Utilities;

public class TheLastWord {

    public static void main(String[] args) {
        new TheLastWord();
    }

    public TheLastWord() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JTextArea ta = new JTextArea(10, 20);
            add(new JScrollPane(ta));
            JLabel lastWord = new JLabel("...");
            add(lastWord, BorderLayout.SOUTH);

            ta.getDocument().addDocumentListener(new DocumentListener() {

                @Override
                public void insertUpdate(DocumentEvent e) {
                    checkLastWord();
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    checkLastWord();
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    checkLastWord();
                }

                protected void checkLastWord() {
                    try {
                        int start = Utilities.getWordStart(ta, ta.getCaretPosition());
                        int end = Utilities.getWordEnd(ta, ta.getCaretPosition());
                        String text = ta.getDocument().getText(start, end - start);
                        lastWord.setText(text);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

}

您可以使用像这样的正则表达式 (\w+.?)$,它将匹配字符串的最后一个单词,即使它以 ..

结尾
String sentence = "I am a full sentence";

String replaced = sentence.replaceAll("(\w+.?)$", "replaced");
System.out.println(replaced); // prints 'I am a full replaced'

你可以分裂。并获取最后一个索引,然后插入像这样的子字符串

    String sentence ="Meow test test hello test.";

    String[] temp = sentence.split("[|!|\?|.|\s|\n]");
    String word = temp[temp.length-1];
    int index = sentence.lastIndexOf(word);
    String out = sentence.substring(0,index) + " INSERTED WORD" + sentence.substring(index+word.length(), sentence.length());