[英]create regex to match a string with a letter followed by fixed number of digits
We have one of use case where we need to validate some Uid
with following constraint: 我们有一个用例之一,需要用以下约束来验证一些
Uid
:
Uid of lenght 5 in form annnnn where a is any letter other than
A
,B
,D
andE
.长度为5的形式为annnn ,其中a是除
A
,B
,D
和E
以外A
任何字母。 And n is any number from0
to9
.n是
0
到9
任何数字。
So I have create new method which will validate same and working for as expected. 因此,我创建了一个新方法,该方法将验证相同方法并按预期工作。
/// <summary>
/// Validate Uid of lenght 5 in form annnnn where ‘a’ is any letter other than A, B, D and E.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static bool IsValidUid( string value )
{
bool returnValue = false;
if (string.IsNullOrEmpty(value) == false && value.Length == 5)
{
char firstChar = value[0];
bool firstCharFailed = false;
switch (firstChar)
{
case 'A':
case 'B':
case 'D':
case 'E':
firstCharFailed = true;
break;
}
if (firstCharFailed == false)
{
string remainingStr = value.Substring(1, 4);
int numb;
if (int.TryParse(remainingStr, out numb) == true)
{
returnValue = true;
}
}
}
return returnValue;
}
But it could be effectively done via regular expression. 但这可以通过正则表达式有效地完成。 Since I am bad with it, I need help to create one for me!
由于我对它不好,因此需要帮助为我创建一个! Thanks in advance!!
提前致谢!!
where 'a' is any letter other than A, B, D and E.
其中“ a”是除A,B,D和E以外的任何字母。
So in this case you need to specify the set of letters that are allowed 因此,在这种情况下,您需要指定允许的字母集
[a-zF-ZC]
=> meaning all letters are in the range az and FZ should be matched, also C should be matched [a-zF-ZC]
=>表示所有字母均在az和FZ范围内,应同时匹配C
And 'n' is any number from 0 to 9.
“ n”是从0到9的任何数字。
\\d
matches a digit or you can use [0-9]
\\d
与数字匹配,或者您可以使用[0-9]
of lenght 5 in form annnnn
长度为5的表格,格式为annnnn
\\d{4}
specifies the amount of the preceding character that is ought to be matched \\d{4}
指定应该匹配的前一个字符的数量
One last thing has to be done. 最后一件事必须要做。 You need to specify, that
a
is supposed to be at the beginning of the string and that the string ends after the 5-th character: 您需要指定
a
应该在字符串的开头,并且字符串在第5个字符之后结束:
^
denotes the start of a string ^
表示字符串的开头
$
denotes the end of a string. $
表示字符串的结尾。
So combined it would look like this: 所以结合起来看起来像这样:
string pattern = @"^[a-zF-ZC]\d{4}$
or using the range for the digits: 或使用数字范围:
string pattern = @"^[a-zF-ZC][0-9]{4}$
Here is an overview of the regex patterns with examples and explanations 这是正则表达式模式的概述,并带有示例和解释
Here is a site where you can test and try out your regex pattern . 这是一个可以测试并试用正则表达式模式的站点 。 Fiddle around with it to get to know it.
弄弄它以了解它。 It helps
它有助于
EDIT: 编辑:
the [...]
denote a set of characters that will be matched. 的
[...]
表示一个字符集,这将被匹配。 A Range is denoted using the -
sign like in az
, AF
or 5-9
. 范围是使用表示
-
标志像az
, AF
或5-9
。 Any other character ca be simply put into the set: 任何其他字符都可以简单地放入集合中:
[abcdefgxyz]
will match only those specified letters! [abcdefgxyz]
仅匹配指定的字母!
The set and the ranges can be combined 设置和范围可以组合
[CF-SX-Z]
will match C
or all capital letters between F
and S
and between X
and Z
. [CF-SX-Z]
将匹配C
或F
和S
之间以及X
和Z
之间的所有大写字母。 I hope it get more clear now 我希望它现在变得更清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.