{"id":3191,"date":"2024-02-21T18:02:58","date_gmt":"2024-02-21T18:02:58","guid":{"rendered":"https:\/\/www.bardecode.com\/en1\/?p=3191"},"modified":"2024-02-21T18:02:58","modified_gmt":"2024-02-21T18:02:58","slug":"basic-guide-regular-expressions","status":"publish","type":"post","link":"https:\/\/www.bardecode.com\/en1\/basic-guide-regular-expressions\/","title":{"rendered":"A basic guide to regular expressions"},"content":{"rendered":"<p>Wikipedia defines a regular expression as &#8220;a sequence of characters that specifies a match pattern in text&#8221;.<\/p>\n<p>Another way is just to think of it as a way of describing text.<\/p>\n<p>For example, a UK number plate currently has the format of 2 letters, followed by 2 numbers, followed by 3 letters. As a regular expression this can be written as:<\/p>\n<p>[A-Z]{2}[0-9]{2}[A-Z]{3}<\/p>\n<p>So this would match the string AB12CDE but would not match AB1CDE<\/p>\n<p>Strangely this would also match ABC12DEFG because it contains the correct pattern (BC12DEF). If you want to exclude such strings then you can put a ^ at the start and a $ at the end:<\/p>\n<p>^[A-Z]{2}[0-9]{2}[A-Z]{3}$<\/p>\n<p>^ is a token for the start of a string and $ is a token for the end of the string.<\/p>\n<p>[] is used to specify a set of characters or a range&#8230;<\/p>\n<p>[a-zA-Z] means any lower or upper case character<br \/>\n[0-5] means any digit in the set 0, 1, 2, 3, 4, 5 and is the same as [012345]<br \/>\n[a6gW] means any character in the set a, 6, g or W<\/p>\n<p>{} is used to specify the number of times the previous token should be repeated, {N} just means N times but {N, M} means between N and M times (inclusive). You can also use the simple + for 1 or more or * for 0 or more (subtle difference!).<\/p>\n<p>e.g [A-Z]+[0-9]+[A-Z]+ would match ABC123DEF but would not match ABC123, on the other hand [A-Z]+[0-9]+[A-Z]* would match both.<\/p>\n<p>There are a few short cuts such as \\d to represent [0-9] but these can vary depending on whether you&#8217;re working in perl or java.<\/p>\n<p>BardecodeFiler uses regular expressions in the Reformat Table, which is a way of changing the information in a barcode into a file name. One extra concept you need to understand to use this is grouping using ().<\/p>\n<p>In our original number plate example you could divide it into 3 groups:<\/p>\n<p>^([A-Z]{2})([0-9]{2})([A-Z]{3})$<\/p>\n<p>So for the string AB12CDE this would mean that AB was the first group, 12 the second and CDE the third. <\/p>\n<p>You now have the power to pull a string apart and put it back together in a different order.<\/p>\n<p>Back to BardecodeFiler and the Reformat table &#8211; it has a number of rows and each row has a left side and a right side. The left side is a regular expression and the right side is the new format where {1} is a token for group 1, {2} for group 2 etc.<\/p>\n<p>For example, say we want to rewrite AB12CDE as CDE12AB<\/p>\n<p>Left side: ^([A-Z]{2})([0-9]{2})([A-Z]{3})$<br \/>\nRight side: {3}{2}{1}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wikipedia defines a regular expression as &#8220;a sequence of characters that specifies a match pattern in text&#8221;. Another way is just to think of it as a way of describing text. For example, a UK number plate currently has the format of 2 letters, followed by 2 numbers, followed by 3 letters. As a regular<\/p>\n<p><a href=\"https:\/\/www.bardecode.com\/en1\/basic-guide-regular-expressions\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":341,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[196,18],"tags":[],"class_list":["post-3191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bardecodefiler","category-knowledge-base"],"_links":{"self":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/3191"}],"collection":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/comments?post=3191"}],"version-history":[{"count":1,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/3191\/revisions"}],"predecessor-version":[{"id":3192,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/3191\/revisions\/3192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media\/341"}],"wp:attachment":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media?parent=3191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/categories?post=3191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/tags?post=3191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}