0

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;
}
Discipulos
  • 423
  • 8
  • 23

1 Answers1

0

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 (\).

Kleo G
  • 247
  • 1
  • 4
  • 20
  • I have modified the question since the matched word is "<" followed by a non alphabetic char. – Discipulos Aug 17 '17 at 14:50
  • Is it only one character that is being added on to the end of it? If so, have you tried to limit the match to only a single character (in this case just the '<'), as it should ignore the character after? – Kleo G Aug 17 '17 at 15:08
  • one or more character, I have tried '<' with no results. – Discipulos Aug 17 '17 at 15:10
  • The problem is more complex than it seems, I'm modifying the question. – Discipulos Aug 18 '17 at 10:20
  • Okay, i understand now, however, this regex should work. Use this: [link]http://www.regexplanet.com/advanced/java/index.html Put the regex in at the top (without double backslash only a single) it will convert it into java regex for you. Have your inputs at the bottom (with and without spaces), and you will see that the regex is fine, i just tried it for you. Are you sure its not the input you're passing through? – Kleo G Aug 18 '17 at 10:52