A problem with syntax highlighting
I'm writing an IDE for Python language in C++ and I have wrote a syntax highligh开发者_如何转开发ter for Python language syntax. but i have two problems with it:
my regular expression pattern for highkighting numbers is '\d+'. it highlights numers everywhere in code. for example it highlights '4' in 'Qt4' and i want to it doesn't highlight numbers in such cases.
my regular expression pattern for highkighting strings between two '"' is '\".*\"'. it works but with one problem. it even highlights cases as '"some text1" some text2 "some text3"' and i want to it doesn't highlight 'some text2'.
what is solution for these two problems?
As already mentioned in the comments it may not be appropriate to use regular expressions to parse a language.
Regarding your two questions:
- You can circumvent this behaviour by using word boundaries:
\b\d+\b
- Make your expression non-greedy:
\".*?\"
Note: As already mentioned those are only workarounds and may not be correct in any case.
精彩评论