Search code examples
regexswiftlint

Regex that doesn't contain is|has prefix but ends with : Bool


Sorry that I have to ask that, with all the other answers around, but I can't get my head around to think and adapt to my problem.

So I'm writing a custom SwiftLint rule that should check:

  • does not have is or has prefix
  • does have : Bool suffix

My current Regex looks like this, but it shows me the stuff that contains is or has, but I want it to show me things that don't have it

(is|has)[A-Z][A-Za-z]*(: Bool)

I also tried this, but it won't show me any message, so I guess it's wrong:

^(?!is|has)[A-Z][A-Za-z]*: Bool$

So, what's the correct Regex?


EDIT: How it should be:

`isHello: Bool` -> nothing
hello: Bool -> should alarm me, missing is or has prefix

EDIT 2:

@State private var showActionSheet: Bool = false -> should give me a warning
@State private var hasStartWorkout: Bool = false -> should not give me a warning

Solution

  • Your problem is, that in a successful case of the negative lookahead the following regex part is not matching: "^(?!is|has)[A-Z][A-Za-z]*: Bool$". In your example, the line that doesn't start with is/has ("hello") starts with a lower character. So you have to move said part inside of the negative lookahead like in this regex:

    (^| )(?!has[A-Z]|is[A-Z])[A-Za-z]+: Bool