当前位置:   article > 正文

如何在Android中使用正则表达式_android pattern compile

android pattern compile

在android中如何插入正则表达式

百度中关于此方法的讲解链接:
百度:如何在AS中插入正则表达式

//用这三句话就可以判断字符串string是否满足正则表达式regex的格式规范。
//regex表示的是正则表达式
Pattern pattern = Pattern.compile(regex);
//string表示的是需要匹配的字符
Matcher matcher = pattern.matcher(string);
//这边就是直接返回一个boolean值,来表示正确与否
matcher.matcher();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

举个例子:用正则表达式判断字符型数是否为整数

Pattern pattern=Pattern.compile("[0-9]*");
Matcher matcher=pattern.matcher(string);
if(matcher.matches()){
	//结果为数值型执行的操作
}else{
	//结果为非字符型执行的操作
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

第二种用正则表达式判断字符型数是否为整数的方法

Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/286393
    推荐阅读
    相关标签
      

    闽ICP备14008679号