Skip to content

Instantly share code, notes, and snippets.

@pavel-zdenek
Last active January 25, 2017 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavel-zdenek/dbb0bdee70b1a08970568a10bbba1fde to your computer and use it in GitHub Desktop.
Save pavel-zdenek/dbb0bdee70b1a08970568a10bbba1fde to your computer and use it in GitHub Desktop.
Regex to search for force unwraps in Swift code

^(?!(.*?\s+)?(#|\/\/)).*?\w+\!([ .\)]|$)

Breakdown:

^(?!.*?\s+(#|\/\/)\s+) Filter out comments in shellscript and C++ single-line style. C-style multiline would be nice to have but extremely complicated and this already covers 80%

?! Assert nonmatch of (.*?\s+)? Optionally a sequence of any characters followed by at least one whitespace (#|\/\/) followed by hash or double slash

Non greedy forward to the token of interest

\w+\!

Variable name or a keyword, followed by a bang.

([ .)]|$)

Tokens allowed to follow the bang (including EOL). Supposed to filter out bangs as logical negation operators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment