zoukankan      html  css  js  c++  java
  • SWIFT中正则表达式验证邮箱

    在playground内写入以下代码,正则关键字跟其它语言的没什么区别

    class Regex {
        let internalExpression:NSRegularExpression
        let pattern:String
        
        init(pattern:String) {
            self.pattern = pattern
            var error:NSError?
            self.internalExpression = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive, error: &error)!
        }
        
        func match(input:String) -> Bool {
            let matches = self.internalExpression.matchesInString(input, options: nil, range: NSMakeRange(0, count(input)))
            return matches.count > 0
        }
    }
    
    var email_regex = case Email = "^([a-zA-Z0-9]+([._\-])*[a-zA-Z0-9]*)+@([a-zA-Z0-9])+(.([a-zA-Z])+)+$"
    
    var regex = Regex(pattern:email_regex)
    
    regex.match("service@t.com")  //RETURN true
    regex.match("ken.ngai@tao.com.cn") //RETURN true
    regex.match("buddy_wei@frend.org") //RETURN true

     CaseInsensitive:大小写不敏感

  • 相关阅读:
    python二维数组切片
    [转载]MIPS常用指令及实例
    使用vim编程步骤
    数组指针和指针数组
    线程基础
    顶层const和底层const
    递归调用
    输出流
    C++代码规范
    I/O流
  • 原文地址:https://www.cnblogs.com/foxting/p/4638280.html
Copyright © 2011-2022 走看看