Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regex Validation Rule using Regex.Match()

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.

So the string should look like this if its valid:

CCCCNNNNNN or CCCCNNNNNNN

C being any character and N being a number.

My expression is written: @"^[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.Success is true, then does that mean that the string is valid?

like image 550
Shawn J. Molloy Avatar asked Jan 06 '12 21:01

Shawn J. Molloy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

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

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

See: Regular Expression Language - Quick Reference


The Regex.Match Method returns a Match object. The Success Property indicates whether the match is successful or not.

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

if (!match.Success)
{
    // does not match
}
like image 169
dtb Avatar answered Oct 03 '22 00:10

dtb


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.

like image 36
Smi Avatar answered Oct 03 '22 01:10

Smi


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

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

Try it as DotNetFiddle

like image 29
kirti kant pareek Avatar answered Oct 03 '22 01:10

kirti kant pareek