Discipulos
Discipulos

Reputation: 469

Java syntax highlighting mismatched char

I'm implementing a syntax highlighting by reusing the code posted here. The problem is that each char "{,},<,>" is not coloured it is not followed by a non-char. For example in "

@Override
    public void insertString (int offset, String str, AttributeSet a) throws BadLocationException 
      {
            super.insertString(offset, str, a); 
            String text = getText(0, getLength());
            int before = findLastNonWordChar(text, offset);
            if (before < 0) before = 0;
            int after = findFirstNonWordChar(text, offset + str.length());
            int wordL = before;
            int wordR = before;                               
            while (wordR <= after) {
                if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {
                    if (text.substring(wordL, wordR).matches("(\\W)*(SELECT|WHERE)"))
                        setCharacterAttributes(wordL, wordR - wordL, attr, false);
                    else if (text.substring(wordL, wordR).matches("(\\W)*(\\{|\\}|\\<|\\>)"))
                      setCharacterAttributes(wordL, wordR - wordL, attrRed, false);
                    else
                        setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
                    wordL = wordR;
                }
                wordR++;
            }
        }
private int findLastNonWordChar (String text, int index) {
        while (--index >= 0) {
        if (String.valueOf(text.charAt(index)).matches("\\W")) {
            break;
        }
    }
    return index;
}
private int findFirstNonWordChar (String text, int index) {
    while (index < text.length()) {
        if (String.valueOf(text.charAt(index)).matches("\\W")) {
            break;
        }
        index++;
    }
    return index;
}

Upvotes: 0

Views: 117

Answers (1)

Kleo G
Kleo G

Reputation: 247

To match angle brackets in Java 8 regular expressions, you must use

    .*

So use:

    .matches("<.*") 

matches a single angle bracket. Combine this with the regular expression you already have to get a single angle bracket, WITHOUT the double backslash (\).

Upvotes: 0

Related Questions