Hesam Qodsi
Hesam Qodsi

Reputation: 1585

A problem with syntax highlighting

I'm writing an IDE for Python language in C++ and I have wrote a syntax highlighter for Python language syntax. but i have two problems with it:

  1. 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.

  2. 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?

Upvotes: 0

Views: 232

Answers (1)

Howard
Howard

Reputation: 39217

As already mentioned in the comments it may not be appropriate to use regular expressions to parse a language.

Regarding your two questions:

  1. You can circumvent this behaviour by using word boundaries: \b\d+\b
  2. Make your expression non-greedy: \".*?\"

Note: As already mentioned those are only workarounds and may not be correct in any case.

Upvotes: 1

Related Questions