如何制作两位数逗号字符串的正则表达式,如(1,23,45,67),每个数字都在1到95之间 [英] How to make a regex for two digits comma string like (1, 23, 45, 67), every couple of digit is between 1 and 95

查看:144
本文介绍了如何制作两位数逗号字符串的正则表达式,如(1,23,45,67),每个数字都在1到95之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我正在研究mvc vb应用程序。在我的一个模特中,我有这个属性:



 Public Property Ladder As String 





我想用regex验证它,所以有两个数字和逗号表达式,如1,23,45,67这个条件:每两个数字在1-95之间。



我不熟悉正则表达式你能帮帮我吗?



我试过的:



到目前为止,我在互联网上找到了以下正则表达式:^。[0-9,] * $只有在表达式由数字和逗号,但这不是我想要的。

解决方案

只有当表达式由数字和逗号组成时才给出,但这不是我想要的。


不要尝试进行值比较 - 即每两位数在1到95之间 - 因为正则表达式不是值处理器:它们是文本处理器,而它是可以这样做:

(1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ... | 93 | 94 | 95)

它很麻烦且难以维护 - 特别是当你想要将字符串验证为逗号分隔时,你需要将值识别位包括两次。



只识别逗号分隔的一个或两个字符串数字很​​简单:

 ^ \d {1,2}(,\d {1,2})* 


< blockquote>

但是......最好用你的演示语言来检查这个值。


Hi, everyone. I am working on mvc vb application. In one of my models I have this property:

Public Property Ladder As String



I want to validate it with regex so to have two digits and comma expression like 1,23,45,67 with this condition: every two digits are between 1-95.

I am not familiar with regex can you help me?

What I have tried:

So far I found in internet the following regex: ^.[0-9,]*$ which gives true only if the expression consists of digits and commas but this is not what I want.

解决方案

which gives true only if the expression consists of digits and commas but this is not what I want.


Don't try to do "value" comparisons - i.e. "every two digits are between 1 and 95" - because regular expressions aren't "value processors": they are text processors and while it is possible to do:

(1|2|3|4|5|6|7|8|9|10|11| ... |93|94|95)

it's messy and difficult to maintain - particularly when you want to validate a string as comma delimited so you need to include the "value recognition" bit twice.

Just recognising a comma delimited string of one or two digits is easy:

^\d{1,2}(,\d{1,2})*


But ... checking the value is best done in your presentation language.


这篇关于如何制作两位数逗号字符串的正则表达式,如(1,23,45,67),每个数字都在1到95之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆