开发者

Regular expression for barcodes (12 or 14 digits)

开发者 https://www.devze.com 2023-03-31 15:06 出处:网络
I\'m looking for regular expression which will match barcode.The barcodes can be 12 or 14 digits long (never 13) and the first digit has to be 8.Examples:

I'm looking for regular expression which will match barcode. The barcodes can be 12 or 14 digits long (never 13) and the first digit has to be 8. Examples:

800909887898  
80903243234323

I've got something like this: ^[8]{1}[0-9]{11}$ but I don't know hot to recognize last 2 di开发者_JAVA技巧gits. It's can be 2 digits character or null (the expression [0-9]{0-2} will not work fine because quantifier don't work like OR).


Check this one

^8[0-9]{11}([0-9]{2})?$

[0-9]{2} means two digits

(...)? means zero or one time


So, if you want something that starts with 8 and can be exactly 12 or 14 digits, then you can use | for an "or" in the regex:

^8\d{11}$|^8\d{13}$

0

精彩评论

暂无评论...
验证码 换一张
取 消