# Matches C/C++ #if/#ifdef/#ifndef .. #endif (?m)(?if[^\s]+) # Match (and the rest of the word) (?.+?$) # the is anything until the end of the line # Now things get tricky. Because ifdefs can nest, we need a balancing group (?> [^\#]+ # Any non-preprocessor character matches, and doesn't change the balance | (?) # An if Increases the | (?) # An EndIf Decreases the Depth | \# # Match any remaining )*(?(Depth)(?!)) # Match Until EndIf is balanced (?