user1127051
user1127051

Reputation: 117

Exclude a certain number from this regex

I have this regex that test for an input with max length of 5. My problem is I want to exclude the single digit of 2. If string contains only number "2", it should fail. How do I exclude number 2 in this regex? /^([a-zA-Z,\d]){1,5}$/

13425 - Match
03277 - Match
2 - Fail.

Upvotes: 2

Views: 3295

Answers (2)

abdelhadi
abdelhadi

Reputation: 305

I was having the same issu, i need to match all number with five degit except those :

    1. from 47893 to 47899

      23030 , 22060, 21037 and 21050

    1. To match all number with five degit : ^\\d{5}$

      To exclude number from 47893 to 47899 :`?!(^4789[3-9]$)`

      3- To exclude other number : ?!(23030|22060|21037|21050)
  • -->The final pattern : (?!((^4789[3-9]$)|23030|22060|21037|21050))^\\d{5}$

    Upvotes: -1

    Tomalak
    Tomalak

    Reputation: 338148

    A negative look-ahead assertion can do this for you

    /^(?!2$)([a-zA-Z,\d]){1,5}$/
    

    Upvotes: 4

    Related Questions