1. Home
  2. Regular Expressions

Regular Expressions

Overview

This article describes the barcode regular expressions feature that provides a very flexible mechanism by which scanned barcode data can be manipulated by the StayLinked Server before that data is entered into the Telnet Session. StayLinked has always provided barcode data manipulation capabilities, but the barcode regular expression feature provides the ultimate flexibility and capability to manipulate scanned barcode data.

What are Regular Expressions?

Regular expressions provide the ability to describe and identify specific patterns of words or characters in strings of text. In the StayLinked context, the strings of text are the data in the scanned barcodes. Using regular expressions, you can describe a pattern to match the text in scanned barcode data. The pattern is specified using a pre-defined syntax that is processed by the Java-based regular expression parser inside the StayLinked server. If a match is identified, then a replacement string can be processed which is used to manipulate and transform the scanned barcode data into its desired form.

So, when using regular expressions with StayLinked, you will provide an ‘Expression’ string which uses regular expression syntax (Java – similar to PERL) to describe a pattern to be matched in the scanned barcode data. You will also provide a ‘Replacement’ string which describes how to manipulate the scanned barcode data and transform that data into the desired format.

To learn more about regular expressions, we recommend reviewing these resources:
http://en.wikipedia.org/wiki/Regular_expression
www.Regex101.com

Composing an ‘Expression’ to match a pattern in barcode data

An ‘expression’ is a pattern that is composed of simple characters, such as /123/, or a combination of simple and special characters, such as /12*3/ or ^PART(\d+)$. The last example uses ‘capturing parentheses’ to identify some of the data in the pattern. The barcode data that matches the part of the pattern inside the ‘capturing parenthesis’ is stored in memory for later use in the ‘Replacement’ string.

Simple Patterns used in an ‘Expression’

Simple patterns are constructed of characters for which you want to find a direct match. For example, the pattern /BIN/ matches character combinations in barcode data only when exactly the characters “BIN” occur together and in that order. Such a match would succeed in the barcodes "BIN98654" and "LOC55BIN99". In both cases the match is with the exact characters “BIN”. There is no match found in the barcode data "BOX55967LOT559" because it does not contain the exact characters “BIN”.

Special Characters used in an ‘Expression’

When the search for a matching pattern requires something more than a direct match, such as finding one or more B's, or finding white space, then you can include special characters in the expression. For example, the pattern /12*3/ matches any character combination in which a single “1” is followed by zero or more “2”s (* means 0 or more occurrences of the preceding item) and then immediately followed by “3”. In the string "3221222234523," the pattern matches the substring “122223”.

The following matrix provides a complete list and description of the special characters that can be used in your expressions.

Using Parenthesis to make Capturing Groups

The ‘Expression’ string that you compose has two functions. First, the expression is used to match a pattern of characters in the scanned barcode data. Second, the expression identifies any parts of the scanned barcode data that you want to be included in the ‘Replacement’ string that will ultimately be entered into the telnet session.

When the scanned barcode data matches an ‘Expression’, those parts of that barcode data that you want to use in the ‘Replacement’ string are indicated by those parts of the ‘Expression’ string that are surrounded by parenthesis to make ‘capturing groups’. Any scanned barcode data enclosed in a ‘capturing group’ is remembered in memory and can be referred to in the ‘Replacement’ string.

Your ‘Expression’ can include zero of more of these ‘capturing groups’. The data identified by the ‘capturing group’ are stored in memory and are identified sequentially; [1], [2], …, [n]. In your ‘Replacement’ string, you can refer to these remembered data using $1, $2, …, $n syntax. Also note that in Screen Recognition, 'capturing groups' from previous screens can be used with the mnemonic [regexgroup #], where # is the sequential group as noted above. [regexgroup 0] will clear the previously captured groups from memory.

For example, the expression ^PART(\d+)$ matches any scanned barcode data that starts with (^) the text  PART and ends with ($) 1 or more numeric digits (\d+). The numeric digits portion of the expression is enclosed in the ‘capturing group’ and will be remembered in memory. This remembered data can be referred to in the Replacement string using $1. If the scanned barcode data was PART1234567890, then the expression would match this data and the ‘capturing group’ would remember the value of  1234567890. If the ‘Replacement’ string were defined as $1, then the result of the regular expression replacement processing would be 1234567890 which would be sent to the telnet session. If the ‘Replacement’ string were defined as ITEM$1, then the scanned barcode data would be transformed into ITEM1234567890 which would be sent to the telnet session.

The following examples show the ‘Add Regular Expression’ dialog. Using this dialog, you can compose your Expression and Replacement strings. You can even enter Sample Barcode Data to test your individual Regular Expressions in real time.

Creating and Testing Regular Expressions

You can assign barcode rules to either ‘Device Groups’ or to ‘Telnet Host Entries’. To specify Regular Expressions for your barcodes, you must add a specific rule for the specific ‘Barcode Type’ that is being scanned. In this example, we have added a barcode rule to the ‘Default Device Group’ for the Code 3 of 9 barcode type with any length (Min. and Max. Length equals 0).

In this dialog you will find the ‘Regular Expression Processing’ listing which provides the ability to define multiple regular expressions to be processed against the scanned barcode data that meets the criteria defined by this barcode rule. When scanned barcode data is received by the server, the barcode rule that most closely matches the ‘Barcode Type’ and the ‘Min. and Max. Lengths’ will be processed. Within this barcode rule, all of the barcode manipulation functions are processed in dialog order, from the top down. So, ‘Regular Expression Processing’ is applied after ‘Use First ## Digits’ and before ‘Scan Prefix’ and ‘Scan Suffix’. Additionally, if you define multiple ‘Regular Expressions’ to be processed, then they are processed in order and the result of the current expression is used as the data for the following expression. In this way, it is possible that a scanned barcode could be transformed by one or more of the defined regular expressions with the ultimate result being entered into the telnet session.

Because the ‘Regular Expressions’ functionality is so powerful and flexible, there is really no need to use any of the other barcode manipulation functions. All of those functions can be accomplished using ‘Regular Expression Processing’ alone.

You can right-click in the Regular Expression list to access a context menu which provides the following options:

Add – Adds a new regular expression to the list.

Edit – Edit and test the selected regular expression.

Copy – Copy the selected regular expression to the clip-board.

Paste – Paste the copied regular expression into a new ‘Paste’ dialog.

Delete – Delete the selected regular expression.

Move Up – Change the order of regular expression processing by moving the selected expression up in the list.

Move Down – Change the order of regular expression processing by moving the selected expression down in the list.

Import – Import Regular Expressions that were previously exported into a Regex File (*.rgx).

Export – Export the currently defined list of Regular Expressions into the desired Regex File (*.rgx).

Test Expressions – Opens a testing dialog that will allow you to test various sample scanned barcode data and review the results as that data is processed by each of the regular expressions in the current order.

When you add or edit an expression, you will use this dialog:

Expression – Compose your Expression string in this text box.

Replacement – Compose your Replacement string in this text box.

Sample Barcode Data – Enter some sample barcode data in this text box.

Results – This text box will display the results of the ‘Expression’ string and the ‘Replacement’ string after processing against any ‘Sample Barcode Data’ that you have entered. The ‘Results’ value is updated in real time as any of the other elements are changed.

When you select the ‘Test Expressions’ option, you will be presented with this dialog:

This dialog will display the currently defined Regular Expressions. The Expression and Replacement strings will be displayed in their respective columns. As you type in some sample barcode data, the Data and Result columns will be updated in real time.

It is important to remember that multiple regular expressions are processed in order with the result of the current expression used as the data for the next expression. In the example above, you can see that the sample barcode data was transformed by the first regular expression and the result became the data for the next regular expression. But, the next regular expression had no effect on that data because there was no pattern match.

Examples of Regular Expressions

Following are several examples of Regular Expressions used to transform scanned barcode data for some common uses:

This regular expression removes the last character from any alpha-numeric barcode data of at least one character.

This regular expression removes the first and the last character from any alpha-numeric barcode data of at least two characters.

This regular expression removes all white space from the scanned barcode data.

This regular expression will provide only the first five characters of scanned barcode data of any length.

This regular expression will replace every occurrence of an asterisk (*) with an upper-case X.

This regular expression will replace every occurrence of the word PART with the word ITEM.

This regular expression will match a barcode that starts with ]C1229 followed by a group of five digits ($1) followed by a 3 followed by a group of 6 digits ($2) followed by a group of 5 to 10 digits ($3) and ending with any one digit. The returned value will be the group of 5 to 10 digits ($3).

Updated on April 13, 2022