user2026794
user2026794

Reputation: 123

Regular Expression not working

I am new to C# so please don't mind some basic mistakes. I am trying to search a pattern of 2 characters and about 4 numbers in strings. In first case it finds the string "UM2345678", but when I have same in longer string, it does not find the same string "UM2345678". Any idea?

Also, if I want to search for particulary UM and any 4 numbers then what will be the pattern?

Thanks.

 namespace StringSearch
 {
class TestRegularExpressionValidation
{
    static void Main()
    {
        string[] numbers = 
    {
        "123-555-0190", 
        "444-234-22450", 
        "690-555-0178", 
        "146-893-232",
        "146-555-0122",
        "4007-555-0111", 
        "407-55-0111",
        "a1b-Cd-EfgH",
        "a1b-Cd-Efgn",
        "UM2345678",
        "11/12/2013 4:10:06 PM                                 UM2345678",
        "407-2-5555", 
    };
        string sPattern = "^[A-Za-z]{2}[0-9]{4}";


        foreach (string s in numbers)
        {
            System.Console.Write("{0,14}", s);
            Match m = Regex.Match(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            if (m.Success)
            {
                System.Console.WriteLine(" - valid");
            }
            else
            {
                System.Console.WriteLine(" - invalid");
            }


        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
  }
}

Upvotes: 0

Views: 257

Answers (5)

Travis
Travis

Reputation: 646

I'm guessing the line you refer to as a "longer string" is this one: "11/12/2013 4:10:06 PM UM2345678"

The reason your regex isn't matching it, is because of the '^' character. That is a meta character that says "start matching at the beginning of a line". If you remove that from your regex, then it should match the above string.

As for your second question, change the [a-zA-Z] to just be UM.

A helpful tool for testing regexes in .NET is Expresso, found here

Upvotes: 2

m1kael
m1kael

Reputation: 2851

Use this for any input (locates 2 letters followed by 4 digits):

[a-zA-Z]{2}\d{4}

And this for your special case:

UM\d{4}

Upvotes: 0

Chris
Chris

Reputation: 339

I am trying to search a pattern of 2 characters and about 4 numbers in strings. In first case it finds the string "UM2345678", but when I have same in longer string, it does not find the same string "UM2345678". Any idea?

Remove the "^".It means the following pattern is at the beginning of the string.

string sPattern = "[A-Za-z]{2}[0-9]{4,}";

Also, if I want to search for particulary UM and any 4 numbers then what will be the pattern?

Try this:

string sPattern = @"UM\d{4}";

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20640

Add a comma:

^[A-Za-z]{2}[0-9]{4,}

That means 4 or more numbers.

Upvotes: 0

gpmurthy
gpmurthy

Reputation: 2427

You regex pattern has a ^ in the beginning which means that you are looking for the pattern from the start of the string. Try changing your pattern as follows...

[A-Za-z]{2}[0-9]{4}

Good Luck!

Upvotes: 0

Related Questions