I am trying to create a simple IDE and colorize my JTextPane based
- Lines ("")
- Comments (// and / * * /)
- Keywords (public, int ...)
- Numbers (integers such as 69 and floating as 1.5)
The way I break the source code by overwriting the insertString and removeString methods inside StyledDocument.
After much testing, I filled out the comments and keywords.
Q1: Regarding coloring the strings, I am breaking the strings based on this regular expression:
Pattern strings = Pattern.compile("\"[^\"]*\""); Matcher matcherS = strings.matcher(text); while (matcherS.find()) { setCharacterAttributes(matcherS.start(), matcherS.end() - matcherS.start(), red, false); }
This works 99% of the time, unless my line contains a certain type of line that has the code "\ inside code". This will ruin the entire color coding. Can someone fix my regex to fix my mistake?
Q2: Regarding integers and decimal coloring, numbers are determined based on this regular expression:
Pattern numbers = Pattern.compile("\\d+"); Matcher matcherN = numbers.matcher(text); while (matcherN.find()) { setCharacterAttributes(matcherN.start(), matcherN.end() - matcherN.start(), magenta, false); }
Using the regular expression "\ d +", I only process integers, not floats. Also, integers that are part of another line are matched, which is not what I want in the IDE. Which correct expression is used for integer color coding?
Below is a screenshot: 
Thanks for any help in advance!