数据注释正则表达式 [英] Data annotation regular expression
问题描述
我需要一个正则表达式来表示这样的字符串:
I need a regular expression for a string like this:
例如1234-1234-12345
ex. 1234-1234-12345
前两个数字必须在01-18之间,并且整个字符串必须为15个字符长
where the first two numbers must be between 01-18 and the whole string must be 15 characters long
示例:0511-xxxx-xxxxx.
example: 0511-xxxx-xxxxx.
我尝试使用 [RegularExpression(@"^ [0-9] {1,18} $",ErrorMessage =无效的ID.")]
但是它不起作用,它甚至给我一个错误,说','丢失了.
but it doesnt work, it even gives me an error that says ',' is missing.
让它变得更容易,它是一个13位字符的数字字符串,其中前两位必须在01-18之间.
Lets make it even easier, a numeric string 13 character long where the first two digits must be between 01-18.
例如1234567890123
Ex. 1234567890123
(我更喜欢第一种格式,但这种格式也可以).
(I would prefer the first format but this one work too).
我不知道如何使用Regex,所以如果有人可以给我一个指向我可以学习的地方的链接,我将非常感激.
I don't know how to use Regex so if someone can kindly give me a link to somewhere I can learn I would very much appreciate it.
而且,最重要的是,如果有更好的方法可以解决此问题而无需使用Regex,我也将不胜感激.
And, most importantly, if there is a better way to get around this without using Regex I would appreciate it as well.
显然,我的要求不清楚.我想要的是前两位数字(XXxx-xxxx-xxxxx)为01、02、03、04、05、06、07、08、09、10、11、12、13、14、15、16、17,18.
Apparently, my request is a little unclear. What I want it that the first two digits (XXxx-xxxx-xxxxx) be 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18.
推荐答案
您的前两个数字"有点不清楚,但是如何:
Your "first two numbers" is a little unclear, but how about:
var pattern = @"(0\d|1[0-8])\d\d-\d{4}-\d{5}";
如果您要匹配整个字符串而不仅仅是找到子字符串,则需要
If you want to match the whole string and not just find the substring, you need
var pattern = @"^(0\d|1[0-8])\d\d-\d{4}-\d{5}$";
如果您没有用连字符将组分开,请使用:
If you didn't have the groups separated by hyphens, use:
var pattern = @"^(0\d|1[0-8])\d{11}$";
您可以像使用它
Regex.IsMatch(aString, pattern)
这篇关于数据注释正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!