What are the correct regular expressions for syntax highlighting?

后端 未结 2 507
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 12:29

What are the correct regular expressions using NSRegularExpression for syntax highlighting?

The following test must be passed:

code // comment
code /* c         


        
相关标签:
2条回答
  • 2021-01-29 13:06

    Using PCRE, the (?s) modifier is a single-line mode, where . can even match newlines.

    (?ms)(//[^\n]+|/\*.*?\*/)
    

    Live example

    You will notice that the second-to-last line, with nested /* /* */ */ will ONLY match the first /* to the first */, and will not match the */ at the end of the line (which is actually correct behavior).

    0 讨论(0)
  • 2021-01-29 13:16

    I think it's impossible in global sense (not just on the sample). So, regexp cann't be used for syntax highlighting.

    0 讨论(0)
提交回复
热议问题