4s_sa.taM
4s_sa.taM

Reputation: 21

Regex - Alphanumeric specific length and numbers greater zero

i have to check if a string has the following requirements:

I already have built a regex pattern, but for the problem with Alpha values with following numbers greater zero i have problems:

VALID

A000001

AB00001

A100000

A100001

ABCABCA

GR00001

GR12345

INVALID

12345

A000001A

A0000012A

A000000

GR00000

ABCAB00

Regex Pattern: ^(?!^KZ)(?!^HJ)(?!^S)(?!^D)(?!0{7})[A-HJ-Z0-9]{7}$

How to check this values?

A000000

GR00000

ABCAB00

Upvotes: 2

Views: 378

Answers (1)

Tensibai
Tensibai

Reputation: 15784

This regex ^(?!KZ|HJ|S|D|([A-HJ-Z]+)?0+$)[A-HJ-Z0-9]{7}$ would do. See the demo

What the regex does is after start of line ensure none of the following conditions match

  • KZ
  • HJ
  • S
  • D
  • ([A-HJ-Z]+)?0+$ This one ensure you can't have 0 till end of line optionnaly with char before.

It's a modified version of your original negative lookahead, compacted with alternation for clarity.

Upvotes: 1

Related Questions