简体   繁体   English

创建正则表达式以匹配带有字母和后跟固定位数的字符串

[英]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 and E . 长度为5的形式为annnn ,其中a是除ABDE以外A任何字母。 And n is any number from 0 to 9 . n09任何数字。

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 . 范围是使用表示-标志像azAF5-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]将匹配CFS之间以及XZ之间的所有大写字母。 I hope it get more clear now 我希望它现在变得更清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果字符串的长度为2并且包含1个字母和1个数字,则正则表达式匹配 - Regex match if a string has length 2 and contains 1 letter and 1 number 字母v的正则表达式前加下划线…,后跟任意长度的数字 - Regex for letter v preceded by underscore…and followed by any length number 正则表达式,匹配“\\”和“\\”之间长4或5位的字符串 - Regex, match string 4 or 5 digits long between “\” and “\” C#正则表达式匹配号后跟右括号 - C# Regex Match Number Followed by Closing Parenthesis C# 正则表达式 - 匹配某些字符后跟数字/标识符 - C# Regex - Match certain char followed by number/identifier 正则表达式用于字符串中至少一定数量的数字 - Regex for at least a certain number of digits in a string 如何为带有字母和数字的搜索模式创建正则表达式? - How to create Regex for search pattern with letter and number? 用于检查字符串是否以],字母和数字开头的正则表达式模式 - Regex pattern for checking if a string starts with ], a letter and a number C#中的正则表达式:匹配在“ _”或“”或“”之前和/或之后的字符串 - Regex in C#: match string thats preceded and/or followed by “_” or “ ” or “” 正则表达式匹配前两个出现的大写字母,然后匹配几个小写字母 - Regex match first two occurrences of a capital letter followed by several lower case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM