如何避免在以特定符号[java]开头的行中突出显示字符串

时间:2016-04-08 14:49:32

标签: java swing highlight jtextarea

我正在创建一个文本编辑器,它在文本的第一行中找到一个字符串,并在整个文本中突出显示它。问题是它还突出显示位于注释行中的事件(以&#34开头;#")。 到目前为止,这是我的代码:

    import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class TextArea {

    public static void main(String[] args) {
        JFrame frame = new JFrame("textArea");
        frame.setSize(500,500);

        final JTextArea textArea = new JTextArea() ;
        frame.add(textArea);

        frame.setVisible(true);

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

            @Override
            public void changedUpdate(DocumentEvent e) {
                try {
                    String keyWords = findKeyWord();
                    findOccurrences(keyWords);
                } catch (BadLocationException ex) {
                    Logger.getLogger(TextArea.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                try {
                    String keyWords = findKeyWord();
                    findOccurrences(keyWords);
                } catch (BadLocationException ex) {
                    Logger.getLogger(TextArea.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                try {
                    String keyWords = findKeyWord();
                    findOccurrences(keyWords);
                } catch (BadLocationException ex) {
                    Logger.getLogger(TextArea.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            public String findKeyWord() throws BadLocationException {
                String keyWord = ""; 

                for( String line : textArea.getText().split("\n")){
                    if (line.trim().length() > 0) {
                        if( !line.startsWith("#") ){                            
                            int keywordEndPosition = line.indexOf("#");
                            keyWord = line.substring(0, keywordEndPosition == -1 ? line.length() : keywordEndPosition);
                            keyWord = keyWord.trim();
                            break;
                        } 
                    }                       

                }                 
            return keyWord;                
            }

            public void findOccurrences(String keyWords) throws BadLocationException {
                Highlighter highlighter = textArea.getHighlighter();
                DefaultHighlighter.DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.CYAN);
                String[] keyArray;
                highlighter.removeAllHighlights();

                keyArray = keyWords.split("[(,)]");
                for (int i=0; i<keyArray.length; i++) {
                    keyArray[i] = keyArray[i].trim();                }

                for (String keyWord : keyArray) {
                    if (keyWord.isEmpty())
                        return;

                    Pattern pattern = Pattern.compile( Pattern.quote(keyWord) );
                    Matcher matcher = pattern.matcher(textArea.getText());  


                    while(matcher.find()) {
                        highlighter.addHighlight(matcher.start(), matcher.end(), painter);
                    }
                }
            }


        });     
    }   
}

如何编辑此代码以避免突出显示位于&#34;#&#34;之后的事件。符号在一条线? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

将图案替换为:

Pattern pattern = Pattern.compile("(?<!#.{0,1000})" + Pattern.quote(keyWord));

我正在使用带有负向lookbehind的正则表达式,#和关键字之间的长度最多为1000个字符。