正则表达式匹配 --所有语言通用

 uri的校验 匹配 http://0.0.0.0:55

       nd = {}
        nd['uuid'] = uuid.encode('utf-8')
        res = re.match(r"(http://){1}([\d]{2,3}([.]{1}" +
            "[\d]{1,3}){3}[:]{1}[\d]{2,5}){1}", uri)
        if res == None:
            return http_client.BAD_REQUEST, {'error': 'uri illegal'}
        if 'gffgfgg' and 'ghrefgfg' not in body.keys():
            LOG.error('create failed with body paras error')
            return http_client.BAD_REQUEST, {'error': 'create error'}
        nd['dcId'] = body['dcId'].encode('utf-8')



变量匹配 小写字母打头  只包含字母和数字的 

            m1 = re.match(r"(^[a-z]){1}", bianliang)
            m2 = re.search(r"([^a-z0-9])+", bianliang)
            if m1 == None and  m2:
                return http_client.BAD_REQUEST, {'error': 'params bianliang illegal!'}


ipv6匹配


  val httpRex:String = "(http|https)://"
  val ipv4Rex:String = "((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}" +
    "(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)"
  val portRex:String = "([1-5]?\\d{1,4}|6[1-5][1-5][1-3][1-5])"


  var ipv6Rex = "([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)" +
    "|::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)" +
    "|([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d" +
    "|[01]?\\d\\d?)|([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]" +
    "|2[0-4]\\d|[01]?\\d\\d?)|([\\da-fA-F]{1,4}:){3}:([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d" +
    "|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)" +
    "|([\\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)" +
    "|([\\da-fA-F]{1,4}:){7}[\\da-fA-F]{1,4}|:((:[\\da-fA-F]{1,4}){1,6}|:)" +
    "|[\\da-fA-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)|([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:)" +
    "|([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3}|:)|([\\da-fA-F]{1,4}:){4}((:[\\da-fA-F]{1,4}){1,2}|:)" +
    "|([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?|([\\da-fA-F]{1,4}:){6}:"


  val ipv4Reg = "^" + httpRex + "(" + ipv4Rex + "):" + portRex + "$"
  val ipv6Reg = "^" + httpRex + "(\\[(" + ipv6Rex + ")\\]):" + portRex + "$"


你可能感兴趣的:(收获)