Basic regex pattern matching
The Softek Barcode Reader SDK and our BardecodeFiler Windows application both use ‘regex’ pattern matching for matching barcode values. We often get asked advice on how to build a regex so we thought a short guide to the basics might be useful.
Here is a simple example:
^\d{5}$
So let’s break it down:
^ means the start of the barcode
$ means the end of the barcode
\d means any digit 0 to 9 and the {5} after means that the digit should be repeated 5 times.
So this will match a barcode with value 12345 but will not match 1234 or 123456
If you miss out the ^ and $ symbols then the barcode just needs to contain 5 digits somewhere. In this case a barcode of value 123456 would also match because it contains 5 consecutive digits.
If you want to match between 4 and 6 digits you would use \d{4,6} rather than \d{5}.
You could also use \d+ to match 1 or more digits or \d* to match 0 to any number of digits.
In addition to \d you could also use:
\s to match anything that is white space
\S to match anything that is not white space
\D to match anything that is not a digit
\w to match A-Z a-z 0-9 and _ (underscore)
\W to match anything that is not included in \w
You can also match a specific set of characters using []. For example, if your barcodes all begin with 3 letters A, B and C in any order followed by 5 digits (e.g ABC12345 or CBA98765) then you could use:
^[A-C]{3}\d{5}$
If your barcodes all start with a fixed pattern such as POD, followed say by 8 digits then you would use:
^POD\d{8}$
The above hardly scratches the surface of what a regex can do but there are plenty of resources available:
For a more extensive basic guide try the following link:
https://ryanstutorials.net/regular-expressions-tutorial/regular-expressions-basics.php
For testing out your regular expression try: