{"id":2498,"date":"2019-07-22T10:01:41","date_gmt":"2019-07-22T10:01:41","guid":{"rendered":"http:\/\/www.bardecode.com\/newsite\/?p=2498"},"modified":"2019-07-22T10:01:41","modified_gmt":"2019-07-22T10:01:41","slug":"basic-regex-pattern-matching","status":"publish","type":"post","link":"https:\/\/www.bardecode.com\/newsite\/basic-regex-pattern-matching\/","title":{"rendered":"Basic regex pattern matching"},"content":{"rendered":"<p>The Softek Barcode Reader SDK and our BardecodeFiler Windows application both use &#8216;regex&#8217; 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.<\/p>\n<p>&nbsp;<\/p>\n<p>Here is a simple example:<\/p>\n<p>^\\d{5}$<\/p>\n<p>So let&#8217;s break it down:<\/p>\n<p>^ means the start of the barcode<\/p>\n<p>$ means the end of the barcode<\/p>\n<p>\\d means any digit 0 to 9 and the {5} after means that the digit should be repeated 5 times.<\/p>\n<p>So this will match a barcode with value 12345 but will not match 1234 or 123456<\/p>\n<p>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.<\/p>\n<p>If you want to match between 4 and 6 digits you would use \\d{4,6} rather than \\d{5}.<\/p>\n<p>You could also use \\d+ to match 1 or more digits or \\d* to match 0 to any number of digits.<\/p>\n<p>&nbsp;<\/p>\n<p>In addition to \\d you could also use:<\/p>\n<p>\\s to match anything that is white space<\/p>\n<p>\\S to match anything that is not white space<\/p>\n<p>\\D to match anything that is not a digit<\/p>\n<p>\\w to match A-Z a-z 0-9 and _ (underscore)<\/p>\n<p>\\W to match anything that is not included in \\w<\/p>\n<p>&nbsp;<\/p>\n<p>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:<\/p>\n<p>^[A-C]{3}\\d{5}$<\/p>\n<p>&nbsp;<\/p>\n<p>If your barcodes all start with a fixed pattern such as POD, followed say by 8 digits then you would use:<\/p>\n<p>^POD\\d{8}$<\/p>\n<p>&nbsp;<\/p>\n<p>The above hardly scratches the surface of what a regex can do but there are plenty of resources available:<\/p>\n<p>For a more extensive basic guide try the following link:<\/p>\n<p><a href=\"https:\/\/ryanstutorials.net\/regular-expressions-tutorial\/regular-expressions-basics.php\">https:\/\/ryanstutorials.net\/regular-expressions-tutorial\/regular-expressions-basics.php<\/a><\/p>\n<p>For testing out your regular expression try:<\/p>\n<p><a href=\"https:\/\/regex101.com\/\">https:\/\/regex101.com\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Softek Barcode Reader SDK and our BardecodeFiler Windows application both use &#8216;regex&#8217; 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. &nbsp; Here is a simple example: ^\\d{5}$ So let&#8217;s break it down: ^ &hellip; <\/p>\n","protected":false},"author":1,"featured_media":309,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[196,18,31],"tags":[],"class_list":["post-2498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bardecodefiler","category-knowledge-base","category-software-development-kits"],"_links":{"self":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/2498"}],"collection":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/comments?post=2498"}],"version-history":[{"count":1,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/2498\/revisions"}],"predecessor-version":[{"id":2499,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/posts\/2498\/revisions\/2499"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/media\/309"}],"wp:attachment":[{"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/media?parent=2498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/categories?post=2498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bardecode.com\/newsite\/wp-json\/wp\/v2\/tags?post=2498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}