editText正则表达式的使用 检查输入是否符合规则?
import Android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; /** * Class which shows how to validate user input with regular expression * * @author FaYnaSoft Labs */ public class Main extends Activity { private EditText editText; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.textId); editText.setText("EditText element"); button = (Button) findViewById(R.id.btnId); button.setText("Check"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(checkString(editText.getText().toString())) { editText.setText("Corect"); } } }); } /** * This method checks if String is correct * @param s - String which need to check * @return value of matching */ private boolean checkString(String s) { return s.matches("\\w*[.](Java|cpp|class)"); } }
?
正则表达式查找字符 文章分类:移动开发 String s_Result="Distance: 2.8km (about 9 mins)"; //Distance parsing Pattern p = Pattern.compile("Distance: (\\d+(\\.\\d+)?)(.*?)\\b"); Matcher m = p.matcher(s_Result); if(m.find()){ MatchResult mr=m.toMatchResult(); f_Distance=mr.group(1);//2.8 m_DistanceUnit=mr.group(3);//km } //Time parsing p = Pattern.compile("about (\\d+(\\.\\d+)?) (.*)\\b"); m = p.matcher(s_Result); if(m.find()){ MatchResult mr=m.toMatchResult(); f_timeEst=mr.group(1);//9 m_timeEstUnit=mr.group(3);//min } 或者 String s_Result="Distance: 2.8km (about 9 mins)"; Pattern p = Pattern.compile("(\\d+(\\.\\d+)?) ?(\\w+?)\\b"); Matcher m = p.matcher(s_Result); while(m.find()){ MatchResult mr=m.toMatchResult(); String value=mr.group(1);//2.8 and 9 come here String units=mr.group(3);//km and mins come here } 正则表达式以过滤特殊字符 在网上找了好久也没找到个合适的正则表达式以过滤特殊字符;自己学习了下,写了两个,实现要求。 Java 代码 // 过滤特殊字符 public static String StringFilter(String str) throws PatternSyntaxException { // 只允许字母和数字 // String regEx = "[^a-zA-Z0-9]"; // 清除掉所有特殊字符 String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……& amp;*()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim(); } @Test public void testStringFilter() throws PatternSyntaxException { String str = "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*). {}+.|.)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符¥……{}【】。,;’“‘”?"; System.out.println(str); System.out.println(StringFilter(str)); } // 过滤特殊字符 public static String StringFilter(String str) throws PatternSyntaxException { // 只允许字母和数字 // String regEx = "[^a-zA-Z0-9]"; // 清除掉所有特殊字符 String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim(); } @Test public void testStringFilter() throws PatternSyntaxException { String str = "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*).{}+.|.)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符¥……{}【】。,;’“‘”?"; System.out.println(str); System.out.println(StringFilter(str)); }
?
1 楼 mcxiaoke 2010-11-02
用InputFilter就可以了
android.text.InputFilter
Known Indirect Subclasses
DateKeyListener, DateTimeKeyListener, DialerKeyListener, DigitsKeyListener, InputFilter.AllCaps, InputFilter.LengthFilter, LoginFilter, LoginFilter.PasswordFilterGMail, LoginFilter.UsernameFilterGMail, LoginFilter.UsernameFilterGeneric, NumberKeyListener, TimeKeyListener
android.text.InputFilter
Known Indirect Subclasses
DateKeyListener, DateTimeKeyListener, DialerKeyListener, DigitsKeyListener, InputFilter.AllCaps, InputFilter.LengthFilter, LoginFilter, LoginFilter.PasswordFilterGMail, LoginFilter.UsernameFilterGMail, LoginFilter.UsernameFilterGeneric, NumberKeyListener, TimeKeyListener