What are the correct regular expressions for syntax highlighting?

后端 未结 2 508
被撕碎了的回忆
被撕碎了的回忆 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).

提交回复
热议问题