Advertisement
Guest User

Untitled

a guest
Jan 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. //## 応用
  2. //# 電子メール
  3. 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])?)*$"
  4.  
  5. extension String {
  6. func isEmail() -> Bool {
  7. do {
  8. let regxp = try NSRegularExpression(
  9. pattern: emailRgx, options: [])
  10. let matches = regxp.matchesInString(
  11. self, options:[], range:NSMakeRange(0, (self as NSString).length))
  12. return (matches.count > 0)
  13. } catch {
  14. return false
  15. }
  16. }
  17. }
  18. var test: String = "hoge@me.com"
  19. print("\(test.isEmail())") //true
  20. test = "s.yamada@gmail.com"
  21. print("\(test.isEmail())") //true
  22. test = "#9s444@gmail.com"
  23. print("\(test.isEmail())") //true
  24. test = "abc.sssss.44444"
  25. print("\(test.isEmail())") //false
  26.  
  27. //# 電話番号(日本:非常に簡単なパターン)
  28. let phoneNoRgx = "^\\d{1,4}-\\d{1,4}-\\d{4}$|^\\d{1,4}\\d{1,4}\\d{4}$"
  29. extension String {
  30. func isPhoneNo() -> Bool {
  31. do {
  32. let regxp = try NSRegularExpression(
  33. pattern: phoneNoRgx, options: [])
  34. let matches = regxp.matchesInString(
  35. self, options:[], range:NSMakeRange(0, (self as NSString).length))
  36. return (matches.count > 0)
  37. } catch {
  38. return false
  39. }
  40. }
  41. }
  42. test = "aa-2222-3333"
  43. print("\(test.isPhoneNo())") //false
  44. test = "03-1234-1234"
  45. print("\(test.isPhoneNo())") //true
  46. test = "090-1234-5678"
  47. print("\(test.isPhoneNo())") //true
  48. test = "08011112222"
  49. print("\(test.isPhoneNo())") //true
  50. test = "04492-12345"
  51. print("\(test.isPhoneNo())") //false
  52.  
  53.  
  54. //# 郵便番号(日本)
  55. let zipRgx = "\\d{3}-\\d{4}|\\d{7}"
  56. extension String {
  57. func isZip() -> Bool {
  58. do {
  59. let regxp = try NSRegularExpression(
  60. pattern: zipRgx, options: [])
  61. let matches = regxp.matchesInString(
  62. self, options:[], range:NSMakeRange(0, (self as NSString).length))
  63. return (matches.count > 0)
  64. } catch {
  65. return false
  66. }
  67. }
  68. }
  69. test = "555-1234"
  70. print("\(test.isZip())") //true
  71. test = "2555-334"
  72. print("\(test.isZip())") //false
  73. test = "2555334"
  74. print("\(test.isZip())") //true
  75.  
  76.  
  77. //# yyyyMMdd
  78. //# hh:mm:ss
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement