0

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

Alexander
  • 59,041
  • 12
  • 98
  • 151

1 Answers1

0

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

Community
  • 1
  • 1
Alexander
  • 59,041
  • 12
  • 98
  • 151
  • Not working for me:( ` }\n }\n // MARK: Header Extension\n extension FilterViewController {` No warning there. Maybe custom rule is formatted wrong? – Severyn Katolyk Apr 23 '18 at 10:05
  • Make a regexr page with the failing test cases and post it here. – Alexander Apr 23 '18 at 15:58
  • @SeverynKatolyk Oops, I had it negated. I had a positive lookbehind (`(?<= ... )`), not a negative look behind as needed (`(?<! ... )`). Fixed! https://regexr.com/3ohcj – Alexander Apr 25 '18 at 16:09