Last active
January 15, 2017 05:40
-
-
Save shoheiyokoyama/fec636e83ddd862396d31aab05e5481f to your computer and use it in GitHub Desktop.
iOSエンジニアの正規表現入門 ref: http://qiita.com/shoheiyokoyama/items/5dc67fdc9e06a9dc5728
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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