0-150带一位小数的成绩正则匹配

KX興 2018-10-25 03:54:34
需求:成绩在0-150包含0,150,分数是带一位小数如99.5
...全文
667 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
方_糖 2018-10-31
  • 打赏
  • 举报
回复
一句话搞定 var reg=/(^1?[0-4]?[0-9]?\.[0-9]$)|(^150.0$)/;
KX興 2018-10-30
  • 打赏
  • 举报
回复
可以了,输入系统自己转义,加了多了2\\,删除就可以了
KX興 2018-10-30
  • 打赏
  • 举报
回复
引用 8 楼 jklwan 的回复:
filter使用,用这个
Pattern p = Pattern.compile("([0-9]|[1-9][0-9]|1[0-4][0-9])(\\.?|(\\.[1-9])*)|150");


好像这个正则,也不OK ,小数点输入不了
jklwan 2018-10-29
  • 打赏
  • 举报
回复
filter使用,用这个
Pattern p = Pattern.compile("([0-9]|[1-9][0-9]|1[0-4][0-9])(\\.?|(\\.[1-9])*)|150");
jklwan 2018-10-29
  • 打赏
  • 举报
回复
引用 6 楼 qXing123456789 的回复:
InputFilter的filter就不能用这个了,当你输入一位时就会触发InputFilter的filter,所以当你想输入120.2这个数时,输入120都没问题,但是输入.时内容为120. 这时候也不匹配,所以你就无法输入了。 我发的这个正则只能在提交的时候校验。如果需要InputFilter中使用需要重新写一个。
KX興 2018-10-26
  • 打赏
  • 举报
回复
[quote=引用 2 楼 jklwan 的回复:]

Pattern p = Pattern.compile("([0-9]|[1-9][0-9]|1[0-4][0-9])(\\.[1-9])*|150");

我这个正则匹配用到安卓 EditText,做个限制输入,用了你上面的 ([0-9]|[1-9][0-9]|1[0-4][0-9])(\\.[1-9])*|150 与我昨天找的一个规则(// String pattern="^(1[1-4][0-9]|150)$|^([1-9][0-9])$|^[0-9]$" ; //0-150 )一样的效果,不能输入小数
jklwan 2018-10-26
  • 打赏
  • 举报
回复

public static boolean isScore(String score) {
        if (score==null || score.length() ==0) return false;
        Pattern p = Pattern.compile("([0-9]|[1-9][0-9]|1[0-4][0-9])(\\.[1-9])*|150");
        Matcher m = p.matcher(score);
        return m.matches();
    }
测试结果

0=true
0.0=false
0.1=true
2.1=true
10=true
10.1=true
01=false
99.5=true
100.1=true
129=true
150=true
150.5=false
200=false
.0为非法,如果需要.0合法用下面的
public static boolean isScore(String score) {
        if (score==null || score.length() ==0) return false;
        Pattern p = Pattern.compile("([0-9]|[1-9][0-9]|1[0-4][0-9])(\\.[1-9])*|150");
        Matcher m = p.matcher(score);
        return m.matches();
    }
KX興 2018-10-26
  • 打赏
  • 举报
回复
[quote=引用 4 楼 jklwan 的回复:]
public class NumberInputFilter implements InputFilter {
private Pattern mPattern;

public NumberInputFilter() {
// String pattern="^(1[1-4][0-9]|150)$|^([1-9][0-9])$|^[0-9]$"; //0-150
String pattern="([0-9]|[1-9][0-9]|1[0-4][0-9])(\\\\.[1-9])*|150"; //0-150
// String pattern="^(\\d?\\d(\\.\\d)?|150)$"; //多为小数
this.mPattern= Pattern.compile(pattern);

}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
if (end > start) {
String destinationString = destination.toString();
String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);
return resultingTxt.matches(this.mPattern.toString()) ? null : "";
}
return null;
}

}
KX興 2018-10-26
  • 打赏
  • 举报
回复
[quote=引用 4 楼 jklwan 的回复:]

holder.mTvScore.setFilters(new InputFilter[]{new NumberInputFilter()});
jklwan 2018-10-26
  • 打赏
  • 举报
回复
引用 3 楼 qXing123456789 的回复:
我这个正则匹配用到安卓 EditText,做个限制输入,用了你上面的 ([0-9]|[1-9][0-9]|1[0-4][0-9])(\\.[1-9])*|150 与我昨天找的一个规则(// String pattern="^(1[1-4][0-9]|150)$|^([1-9][0-9])$|^[0-9]$" ; //0-150 )一样的效果,不能输入小数
怎么用的,不会是在textchanged里面判断的吧
KX興 2018-10-25
  • 打赏
  • 举报
回复
有木有人,沉水了

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧