Android 正则表达式实例
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)); }
相关推荐
flyingssky 2020-08-18
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03