Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //## 応用
- //# 電子メール
- let emailRgx = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
- extension String {
- func isEmail() -> Bool {
- do {
- let regxp = try NSRegularExpression(
- pattern: emailRgx, options: [])
- let matches = regxp.matchesInString(
- self, options:[], range:NSMakeRange(0, (self as NSString).length))
- return (matches.count > 0)
- } catch {
- return false
- }
- }
- }
- var test: String = "hoge@me.com"
- print("\(test.isEmail())") //true
- test = "s.yamada@gmail.com"
- print("\(test.isEmail())") //true
- test = "#9s444@gmail.com"
- print("\(test.isEmail())") //true
- test = "abc.sssss.44444"
- print("\(test.isEmail())") //false
- //# 電話番号(日本:非常に簡単なパターン)
- let phoneNoRgx = "^\\d{1,4}-\\d{1,4}-\\d{4}$|^\\d{1,4}\\d{1,4}\\d{4}$"
- extension String {
- func isPhoneNo() -> Bool {
- do {
- let regxp = try NSRegularExpression(
- pattern: phoneNoRgx, options: [])
- let matches = regxp.matchesInString(
- self, options:[], range:NSMakeRange(0, (self as NSString).length))
- return (matches.count > 0)
- } catch {
- return false
- }
- }
- }
- test = "aa-2222-3333"
- print("\(test.isPhoneNo())") //false
- test = "03-1234-1234"
- print("\(test.isPhoneNo())") //true
- test = "090-1234-5678"
- print("\(test.isPhoneNo())") //true
- test = "08011112222"
- print("\(test.isPhoneNo())") //true
- test = "04492-12345"
- print("\(test.isPhoneNo())") //false
- //# 郵便番号(日本)
- let zipRgx = "\\d{3}-\\d{4}|\\d{7}"
- extension String {
- func isZip() -> Bool {
- do {
- let regxp = try NSRegularExpression(
- pattern: zipRgx, options: [])
- let matches = regxp.matchesInString(
- self, options:[], range:NSMakeRange(0, (self as NSString).length))
- return (matches.count > 0)
- } catch {
- return false
- }
- }
- }
- test = "555-1234"
- print("\(test.isZip())") //true
- test = "2555-334"
- print("\(test.isZip())") //false
- test = "2555334"
- print("\(test.isZip())") //true
- //# yyyyMMdd
- //# hh:mm:ss
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement