0

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
Neeraj
  • 1
  • 1
  • 1
    This question is eerily similar to [this one](http://stackoverflow.com/questions/29344296/regular-expression-for-6-characters). – Biffen Apr 01 '15 at 07:37

1 Answers1

1

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
  • _##*
JonM
  • 1,315
  • 10
  • 14
  • 1
    you can replace `(?:_|#)` with `[_#]` – Ulugbek Umirov Apr 01 '15 at 07:29
  • Number,Alphbates,*(at any position),_(at 2 & 3 Position),#(at 3 & 4 Postion). note:- _ will not come single.it will come always in pair like (a__bcd). same with #. – Neeraj Apr 01 '15 at 07:53
  • 2
    @Neeraj, You will have to explain the requirements in a bit more detail. Would you mind updating your question with a list of expected matches and expected non matches? – JonM Apr 01 '15 at 07:56
  • Matches:- (a__cde),(ab##cd),(******),(abcdef),(123456). Won't match: (a_#cde),(a_##aa),(A__#BC),(*_##**) above four expression contain _ and # single. – Neeraj Apr 01 '15 at 08:02
  • @Neeraj I've updated the answer to match your expected outputs. Feel free to test it with more values. – JonM Apr 01 '15 at 11:03