正则表达式 IP

前端之家收集整理的这篇文章主要介绍了正则表达式 IP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.example.localsocketdemo; import android.app.Activity; import android.os.Bundle; import android.text.InputFilter; import android.text.Spanned; import android.widget.EditText; public class RegularExpressions extends Activity{ private EditText edt1,edt2; private void setIpInputFilter(EditText text) { InputFilter[] filters = new InputFilter[1]; filters[0] = new InputFilter() { public CharSequence filter(CharSequence source,int start,int end,Spanned dest,int dstart,int dend) { if (end > start) { String destTxt = dest.toString(); String resultingTxt = destTxt.substring(0,dstart) + source.subSequence(start,end) + destTxt.substring(dend); /** * Bruce 正则表达式,判断输入的IP/网关/子网掩码是否符合标准 */ if (!resultingTxt.matches("^\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) { return ""; } else { String[] splits = resultingTxt.split("\\."); for (int i = 0; i < splits.length; i++) { if (Integer.valueOf(splits[i]) > 255) { return ""; } } } } return null; } }; text.setFilters(filters); } private void initButton(){ edt1 = (EditText)findViewById(R.id.editText1); edt2 = (EditText)findViewById(R.id.editText2); setIpInputFilter(edt1); setIpInputFilter(edt2); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.regularexpressions); initButton(); } } 原文链接:/regex/362504.html

猜你在找的正则表达式相关文章