Skip to content

Instantly share code, notes, and snippets.

@gauravkeshre
Last active January 13, 2019 19:49
Show Gist options
  • Save gauravkeshre/b37f6a43bbca7647697dd10e476dac1d to your computer and use it in GitHub Desktop.
Save gauravkeshre/b37f6a43bbca7647697dd10e476dac1d to your computer and use it in GitHub Desktop.
Recently, I came accross an excellent validate and visualize Regular Expressions. I thought of implementing a small problem using regex and validate it from there.

Check if a string is a number using regex

Recently, I came accross this excellent tool to validate and visualize Regular Expressions. I thought of implementing a small problem using regex and validate it from there.

REGEX: ^\s*\d*([.]\d+|\d+[.]|\d+[eE]\d+){0,1}\d*\s*$

    func isNumber(_ s: String) -> Bool {
    
        let regex = "^\\s*\\d*([.]\\d+|\\d+[.]|\\d+[eE]\\d+){0,1}\\d*\\s*$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [regex])
        return predicate.evaluate(with: s)
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment