1. 程式人生 > >Android學習之使用正則表示式Pattern與Matcher來判斷字串格式

Android學習之使用正則表示式Pattern與Matcher來判斷字串格式

/** * 主介面 * * @author yuzhentao */ public class MainActivity extends Activity implements View.OnClickListener{ private Context context; private EditText et01, et02, et03, et04, et05; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main
); initView(); } @Override public void onClick(View v) { int viewId = v.getId(); switch (viewId) { case R.id.button_01_activity_main: if (!isPhoneRegex(et01.getText().toString())) { Toast.makeText(context, "手機號碼格式錯誤", Toast.LENGTH_SHORT
).show(); } else { Toast.makeText(context, "手機號碼格式正確", Toast.LENGTH_SHORT).show(); } break; case R.id.button_02_activity_main: if (!isCaptchaRegex(et02.getText().toString())) { Toast.makeText(context, "驗證碼格式錯誤", Toast.LENGTH_SHORT
).show(); } else { Toast.makeText(context, "驗證碼格式正確", Toast.LENGTH_SHORT).show(); } break; case R.id.button_03_activity_main: if (!isEmailRegex(et03.getText().toString())) { Toast.makeText(context, "郵箱格式錯誤", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "郵箱格式正確", Toast.LENGTH_SHORT).show(); } break; case R.id.button_04_activity_main: if (!isAccountRegex(et04.getText().toString())) { Toast.makeText(context, "帳號格式錯誤", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "帳號格式正確", Toast.LENGTH_SHORT).show(); } break; case R.id.button_05_activity_main: if (!isPasswordRegex(et05.getText().toString())) { Toast.makeText(context, "密碼格式錯誤", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "密碼格式正確", Toast.LENGTH_SHORT).show(); } break; } } private void initView() { context = this; et01 = (EditText) findViewById(R.id.edittext_01_activity_main); et02 = (EditText) findViewById(R.id.edittext_02_activity_main); et03 = (EditText) findViewById(R.id.edittext_03_activity_main); et04 = (EditText) findViewById(R.id.edittext_04_activity_main); et05 = (EditText) findViewById(R.id.edittext_05_activity_main); findViewById(R.id.button_01_activity_main).setOnClickListener(this); findViewById(R.id.button_02_activity_main).setOnClickListener(this); findViewById(R.id.button_03_activity_main).setOnClickListener(this); findViewById(R.id.button_04_activity_main).setOnClickListener(this); findViewById(R.id.button_05_activity_main).setOnClickListener(this); } /** * 判斷是否是合法手機號碼(手機號碼段詳見:http://baike.baidu.com/view/781667.htm#2) * * @param phone:手機號碼 * @return boolean */ private boolean isPhoneRegex(String phone) { String phonePattern = "^(((13\\d{1})|(14[57])|(15([0-3]|[5-9]))|(17[6-8])|(18\\d{1}))\\d{8}|170[0,5,9]\\d{7})$"; // Pattern p = Pattern.compile(phonePattern); // Matcher m = p.matcher(phone); // return m.matches(); // 或者 return Pattern.matches(phonePattern, phone); } /** * 判斷是否是合法驗證碼(6位數字) * * @param captcha:驗證碼 * @return boolean */ private boolean isCaptchaRegex(String captcha) { String captchaPattern = "^\\d{6}$"; return Pattern.matches(captchaPattern, captcha); } /** * 判斷是否是合法郵箱 * * @param email:郵箱 * @return boolean */ private boolean isEmailRegex(String email) { String emailPattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; return Pattern.matches(emailPattern, email); } /** * 判斷是否是合法帳號(允許6~18位元組,允許字母數字下劃線) * * @param account:帳號 * @return boolean */ private boolean isAccountRegex(String account) { String accountPattern = "^\\w{6,18}$"; return Pattern.matches(accountPattern, account); } /** * 判斷是否是合法密碼(以字母開頭,允許6~18位元組,允許字母數字下劃線) * * @param password:密碼 * @return boolean */ private boolean isPasswordRegex(String password) { String passwordPattern = "^[a-zA-Z]\\w{5,17}$"; return Pattern.matches(passwordPattern, password); } }