zoukankan      html  css  js  c++  java
  • 将ip地址转换成C段地址的UDF

    将ip地址转换成C段地址的UDF,最重要的是判断IP地址的正则表达式。

    package cn.cnnic.ops.Study;
    
    import java.util.regex.Pattern;
    
    import org.apache.hadoop.hive.ql.exec.UDF;
    
    import cn.cnnic.ops.util.Constants;
    
    /**
     * company: cnnic department: ops author: baoshan change client ip to C network
     * segment
     */
    
    public class GetClientCSegment extends UDF {
        public static void main(String[] args) {
            String client = "210.72.32.1";
            GetClientCSegment gccs = new GetClientCSegment();
            System.out.println(gccs.evaluate(client));
        }
    
        public String evaluate(String client) {
            String ipCSegment = "";
            Pattern pattern = Pattern.compile(
                    "^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]|[*])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]|[*])$");
            boolean flag = pattern.matcher(client).matches();
            String[] arr = client.split(Constants.DOT);
            if (flag) {
                ipCSegment = arr[0] + Constants.DOT_SIMPLE + arr[1] + Constants.DOT_SIMPLE + arr[2] + Constants.DOT_SIMPLE
                        + "0";
            } else {
                ipCSegment = client;
            }
            return ipCSegment;
        }
    }
  • 相关阅读:
    Python之初识模块之序列化模块
    Python之初识模块二
    Python之初识模块
    Python之re模块
    python随笔来源
    Python初识模块之正则表达式
    Python之初识递归
    0.U-boot的简介
    2.11.移植uboot
    2.18.7.VFS简介
  • 原文地址:https://www.cnblogs.com/zhzhang/p/5032700.html
Copyright © 2011-2022 走看看