Skip to content

Instantly share code, notes, and snippets.

@tsraveling
Last active July 14, 2017 19:21
Show Gist options
  • Save tsraveling/5ece8fb896f101d71176ecf94cdf63ca to your computer and use it in GitHub Desktop.
Save tsraveling/5ece8fb896f101d71176ecf94cdf63ca to your computer and use it in GitHub Desktop.
Regex pattern check
let pattern = "^([A-Za-z\\d_]+$)" // this checks for usernames matching alphanumeric + _
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let range = NSMakeRange(0, updatedText.characters.count)
let num_matches = regex.numberOfMatches(in: updatedText, options: [], range: range)
guard updatedText.characters.count == 0 || num_matches == 1 else {
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment