Search code examples
regexswiftlint

SwiftLint Rule for pragma mark


I'm trying to implement custom swiftlint rule to track when before "// MARK:" is less than two new lines. I'm not good in regex. Here is my rule:

    custom_rules:
pragma_mark:
name: "Wrong pagma mark format"
regex: "([^\n\n]?\/\/ MARK:)"
message: "Please leave two lines before // MARK:"
severity: warning

but this regex is wrong. what I do wrong? Maybe swiftlint has already this rule? But I can't find it


Solution

  • You'll need to use a negative-look behind. Here's a starting point:

    (?<!\n\n)\/\/ MARK:
    

    And here is a RegExr page that explains how it works, and offers some test cases.

    You'll probably want to make this case insensitive, and be tolerant for white space before and after the "MARK".