Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static String filterMobileNum(String phoneNum) {
        if (TextUtils.isEmpty(phoneNum))
            return "";

        return checkMobileNum(phoneNum.replaceAll("[^0-9]", ""));
    }

    public static String checkMobileNum(String phoneNum) {
        if (TextUtils.isEmpty(phoneNum))
            return "";

        Pattern p1 = Pattern.compile("^((\\+{0,1}86){0,1})1[0-9]{10}");
        Matcher m1 = p1.matcher(phoneNum);
        if (m1.matches()) {
            Pattern p2 = Pattern.compile("^((\\+{0,1}86){0,1})");
            Matcher m2 = p2.matcher(phoneNum);
            StringBuffer sb = new StringBuffer();
            while (m2.find()) {
                m2.appendReplacement(sb, "");
            }
            m2.appendTail(sb);
            return sb.toString();
        } else {
            return phoneNum;
        }
    }
}