简要
根据百度百科最新手机号码规则,编写的手机号合法判断自定义函数
链接: 百度百科最新手机号码规则.
此处仅实现了11位手机号码的实现方式
代码
import com.bigdata.hive.Tools.StringTools;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
public class UdfIsPhone extends UDF {
@Description(
name = "udf_is_phone",
value = "_FUNC_(str) - Returns str is phone?0:1",
extended = "Example:\n > SELECT _FUNC_(\'15593126538\') FROM dual LIMIT 1;\n return 1 "
)
static String[] regex = {"^1[3,5,9][0-35-9]\\d{8}$","^134[0-8]\\d{8}$","^14[5,7,9]\\d{8}$","^166\\d{8}$","^17[2,3,5,6,7,8]\\d{8}$","^18\\d{9}$"};
public int evaluate(final String s) {
if (StringTools.isNotNull(s)) {
for( int i=0 ;i<regex.length;i++){
if(s.matches(regex[i])){
return 1;
}
}
}
return 0;
}
public static void main(String[] args) {
System.out.println("值为" + new com.bigdata.hive.udf.UdfIsPhone().evaluate(new String("15593126538")));
}
}