Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for barcodes (12 or 14 digits)

Tags:

regex

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 digits. 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).

like image 734
Przemek Nowak Avatar asked Aug 30 '11 16:08

Przemek Nowak


People also ask

How do you match a regular expression with digits?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

What is a barcode regex?

Regular Expressions (Regex) are used for barcode data manipulation. With Regex, complicated string matching and character replacement on barcode data is enabled. The Matches condition tries to compare a Regex with the incoming barcode data. The rule execution only continues if the Regex matches the text.

What is the regular expression for numbers only?

To check for all numbers in a field To get a string contains only numbers (0-9) we use a regular expression (/^[0-9]+$/) which allows only numbers. Next, the match() method of the string object is used to match the said regular expression against the input value.

What is the regular expression for characters?

A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.


1 Answers

Check this one

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

[0-9]{2} means two digits

(...)? means zero or one time

like image 115
George Kastrinis Avatar answered Oct 04 '22 16:10

George Kastrinis