Sara
Sara

Reputation: 97

Regular expression for reference number

I need to write the regular expression to fit a specific code pattern: the string to match has 18 integer characters and I need to check if in the first position there is 0 and in the second one 8 or 9. I wrote this expression, but it doesn't work:

 Regex regex = new Regex(@"^(.{0}[0]{1}[8,9])(^\d{18}$)");
        string compare = "082008014385161873";
        if (regex.IsMatch(compare))
        {
            //true
        } 

Anyone can help me?

Upvotes: 1

Views: 1136

Answers (2)

user189198
user189198

Reputation:

Try this regular expression. It does the following:

  • Verifies first digit as 0
  • Verifies second digit as a 8 or 9
  • Verifies that 16 more digits follow the first 2 digits

.

^0[89]\d{16}$

Upvotes: 1

falsetru
falsetru

Reputation: 369374

Use following regular expression:

^0[89]\d{16}$

Alternative using positive lookahead assertion:

^(?=0[89])\d{18}$

Upvotes: 1

Related Questions