Skip to content

Instantly share code, notes, and snippets.

@shoheiyokoyama
Last active January 15, 2017 05:40
Show Gist options
  • Save shoheiyokoyama/fec636e83ddd862396d31aab05e5481f to your computer and use it in GitHub Desktop.
Save shoheiyokoyama/fec636e83ddd862396d31aab05e5481f to your computer and use it in GitHub Desktop.
iOSエンジニアの正規表現入門 ref: http://qiita.com/shoheiyokoyama/items/5dc67fdc9e06a9dc5728
extension String {
func isNumber() -> Bool {
let pattern = "^[\\d]+$"
guard let regex = try? NSRegularExpression(pattern: pattern) else { return false }
let matches = regex.matches(in: self, range: NSRange(location: 0, length: length))
return matches.count > 0
}
}
let numberString = "0123"
print(numberString.isNumber()) // true
let alphabet = "abcd"
print(alphabet.isNumber()) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment