使用 Regex.Match() 的 C# 正则表达式验证规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8764827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:20:36  来源:igfitidea点击:

C# Regex Validation Rule using Regex.Match()

c#regexvalidation

提问by nocarrier

I've written a Regular expression which should validate a string using the following rules:

我编写了一个正则表达式,它应该使用以下规则验证字符串:

  1. The first four characters must be alphanumeric.
  2. The alpha characters are followed by 6 or 7 numeric values for a total length of 10 or 11.
  1. 前四个字符必须是字母数字。
  2. 字母字符后跟 6 或 7 个数值,总长度为 10 或 11。

So the string should look like this if its valid:

因此,如果字符串有效,则该字符串应如下所示:

CCCCNNNNNN or CCCCNNNNNNN

CCCCNNNNNN 或 CCCCNNNNNNN

C being any character and N being a number.

C 是任意字符,N 是数字。

My expression is written: @"^[0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}$";

我的表情是这样写的: @"^[0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}$";

My regex match code looks like this:

我的正则表达式匹配代码如下所示:

var cc1 = "FOOBAR"; // should fail.
var cc2 = "AAAA1111111111"; // should succeed

var regex = @"^[0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}$";

Match match = Regex.Match( cc1, regex, RegexOptions.IgnoreCase );

if ( cc1 != string.Empty && match.Success )
{
     //"The Number must start with 4 letters and contain no numbers.",
     Error = SeverityType.Error
}

I'm hoping someone can take a look at my expression and offer some feedback on improvements to produce a valid match.

我希望有人可以看看我的表达,并提供一些关于改进的反馈,以产生有效的匹配。

Also, am I use .Match()correctly? If Match.Successis true, then does that mean that the string is valid?

另外,我使用.Match()正确吗?如果Match.Successtrue,那么这是否意味着该字符串有效?

采纳答案by dtb

The regex for 4 alphanumeric characters follows by 6 to 7 decimal digits is:

4 个字母数字字符后跟 6 到 7 个十进制数字的正则表达式是:

var regex = @"^\w{4}\d{6,7}$";

See: Regular Expression Language - Quick Reference

请参阅:正则表达式语言 - 快速参考



The Regex.Match Methodreturns a Matchobject. The Success Propertyindicates whether the match is successful or not.

Regex.Match方法返回一个匹配对象。在成功物业表示是否匹配成功与否。

var match = Regex.Match(input, regex, RegexOptions.IgnoreCase);

if (!match.Success)
{
    // does not match
}

回答by Qtax

Your conditions give:

你的条件给出:

  1. The first four characters must be alphanumeric: [A-Za-z\d]{4}
  2. Followed by 6 or 7 numeric values: \d{6,7}
  1. 前四个字符必须是字母数字[A-Za-z\d]{4}
  2. 后跟 6 或 7 个数值\d{6,7}

Put it together and anchor it:

把它放在一起并锚定它:

^[A-Za-z\d]{4}\d{6,7}\z

Altho that depends a bit how you define "alphanumeric". Also if you are using ignore case flag then you can remove the A-Zrange from the expression.

尽管如此,这取决于您如何定义“字母数字”。此外,如果您使用忽略大小写标志,则可以A-Z从表达式中删除范围。

回答by Bernard

Try the following pattern:

尝试以下模式:

@"^[A-za-z\d]{4}\d{6,7}$"

回答by Smi

The following code demonstrates the regex usage:

以下代码演示了正则表达式的用法:

        var cc1 = "FOOBAR"; // should fail.
        var cc2 = "AAAA1111111"; // should succeed
        var r = new Regex(@"^[0-9a-zA-Z]{4}\d{6,7}$");
        if (!r.IsMatch(cc2))
        {
            Console.WriteLine("cc2 doesn't match");
        }
        if (!r.IsMatch(cc1))
        {
            Console.WriteLine("cc1 doesn't match");
        }

The output will be cc1 doesn't match.

输出将为cc1 doesn't match.

回答by kirti kant pareek

The following code is using a regular expression and checks 4 different patterns to test it, see output below:

以下代码使用正则表达式并检查 4 种不同的模式来测试它,请参见下面的输出:

using System;
using System.Text.RegularExpressions;   
public class Program
{
    public static void Main()
    {
        var p1 = "aaaa999999"; 
        CheckMatch(p1);
        p1 = "aaaa9999999";
        CheckMatch(p1);
        p1 = "aaaa99999999";
        CheckMatch(p1);
        p1 = "aaa999999";
        CheckMatch(p1);
    }

    public static void CheckMatch(string p1)
    {
        var reg = new Regex(@"^\w{4}\d{6,7}$");
        if (!reg.IsMatch(p1))
        {
            Console.WriteLine($"{p1} doesn't match");
        }
        else
        {
            Console.WriteLine($"{p1} matches");
        }
    }
}

Output:

输出:

aaaa999999 matches
aaaa9999999 matches
aaaa99999999 doesn't match
aaa999999 doesn't match

aaaa999999 匹配
aaaa9999999 匹配
aaaa99999999 不匹配
aaa999999 不匹配

Try it as DotNetFiddle

尝试作为DotNetFiddle