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
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:
Details
(?<!:\s)
- a negative lookbehind that fails the match if, immediately to the left of the current location, there is a :
and a whitespace\b
- word boundary\w+
- 1+ word chars(?<!\bas)
- a negative lookbehind that fails the match if, immediately to the left of the current location, there is a whole word as
\b
- word boundary\)*
- 0 or more )
chars!
- a !
char.