Skip to content

Instantly share code, notes, and snippets.

@venj
Created December 22, 2015 08:52
Show Gist options
  • Save venj/8bc7f3f442a9aea343ae to your computer and use it in GitHub Desktop.
Save venj/8bc7f3f442a9aea343ae to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
import Darwin
let regex = UnsafeMutablePointer<regex_t>.alloc(1)
let regexString = "^[[:alnum:]]+$"
let regex_str = regexString.cStringUsingEncoding(NSUTF8StringEncoding)!
if (regcomp(regex, regex_str, REG_ICASE | REG_EXTENDED) != 0) {
print("Error compile regex")
exit(255)
}
let str = "11asdfas22".cStringUsingEncoding(NSUTF8StringEncoding)!
let errorcode = regexec(regex, str, 0, nil, 0)
if (errorcode == 0) {
print("matches found.")
}
else if (errorcode == REG_NOMATCH) {
print("No matches.")
}
else { // REG_ESPACE
print("Out of memory!")
exit(255)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment