swiftregexxcodesubtractionnegate

How to do negate or subtract a regex from another regex result in just one line of regex


I am trying to do a regex string to find all cases of force unwrapping in swift. This will search all words with exclamation points in the entire code base. However, the regex that I already have has included implicit declaration of variable which I am trying to exclude.

This is the regex that I already have.

(:\s)?\w+(?<!as)\)*!

And it works fine. It searches for "variableName!", "(variableName)!", "hello.hello!". The exclusion of force casting also works. It avoids cases like "hello as! UIView", But I am trying also to exclude another cases such as "var hello: UIView!" which has an exclamation point. That's the problem I am having. I tried negative lookahead and negative lookbehind and nothing solved this kind of case.

This is the sample regex I am working on

(:\s)?\w+(?<!as)\)*!

And this is the result

testing.(**test)))!**

Details lists capture **groups!**

hello as! hello

**Hello!**

**testing!**

testing**.test!**

Hello != World

var noNetworkBanner**: StatusBarNotificationBanner!** <-- need to exclude

"var noNetworkBanner**: StatusBarNotificationBanner!**" <-- need to exclude

Solution

  • You may use

    (?<!:\s)\b\w+(?<!\bas)\b\)*!
    

    I added \b word boundaries to match whole words only, and changed the (:\s)? optional group to a negative lookbehind, (?<!:\s), that disallows a : + space before the word you need to match.

    See the regex demo and the regex graph:

    enter image description here

    Details