Neeraj
Neeraj

Reputation: 1

Regular expression with 6 characters in ASP.NET

I need a regular expression in ASP.NET that accept 6 characters and

  1. At second position it should accept only underscore (_) and
  2. At third position it should accept maybe underscore (_) or hash (#) and
  3. At fourth position it should accept only hash (#).

Note: In regex user can only enter: Number, Alphabets or Star (*) at any position instead of above mentioned positions.

Can any one help me out on this?

Like:

  1. a__cde
  2. ab##cd
  3. a_#cde
  4. ******
  5. abcdef
  6. 123456

Upvotes: 0

Views: 52

Answers (1)

JonM
JonM

Reputation: 1374

Try the following expression:

(?i)^(?=^(?:[a-z0-9*]*(?:#{1,2}|_{1,2})[a-z0-9*]*|[a-z0-9*]*)$).{6}$

Will match:

  • a__cde
  • ab##cd
  • ******
  • abcdef
  • 123456

Won't match:

  • a_#cde
  • a_##aa
  • A__#BC
  • _##*

Upvotes: 1

Related Questions