skd
skd

Reputation: 165

pattern for json schema validation starting with numbers and ending with K,M and G

Need a pattern for json schema validator where a string starts with a number and ends with K or M or G,

Say the string should be in format 1267 or 142K or 176M or 185G.

Upvotes: 1

Views: 1689

Answers (2)

Bhaskar13
Bhaskar13

Reputation: 371

The pattern ^[0-9]\d*[KGM] by Ashutosh Sharma also accepts with leading zeros

e.g: 0000K is a valid input for ^[1-9]\d*[KGM]$

(Regular expression is unanchored in json schema as Relequestual pointed out, so added $ at end)
The second pattern ^[1-9]\d*[KGM] suggested doesn't accept 0K as valid input


So to accept 0K as well and not to accept digits with leading zeros(other than 0)
^(0|[1-9]\d*)[KMG]$ should be used

Details
0 boolean or 1-9 followed by any number of digits(0-9) and then followed by K or M or G

Upvotes: 3

Ashutosh
Ashutosh

Reputation: 1107

Below pattern works fine for the use case mentioned

^[0-9]\d*[KGM]  //Number might start with zero's.

^[1-9]\d*[KGM]  //Number will not be having preceding zero's.

Upvotes: 1

Related Questions