Regex Matching
PLEASE NOTE THAT THIS IS STILL A BETA FEATURE.
In the Workflow Rules section of ProGlove Connect, Regular Expressions (Regex) can be used for barcode data manipulation and more. With these Regex complicated string matching and character replacement on barcode data is possible.
Conditions
In a rule condition, the "Matches" condition will match a regex against the incoming barcode data. The rule execution will only continue if the Regex matches the text.
Replace (Regex) Action
This action can be used to carry out character replacements, deletions and so on.
In a first step, a RegEx is matched to the barcode string - the "Match" field.
Then, the replacement in "Data" is applied.
Intro to Regex
Generally, Regex can contain special characters with a meaning and normal letters. This is a very short intro. For a more thorough understanding, you can for example consult the guide at regexone.
Regex are created by combining different elements together:
- Characters, numbers, etc can be just written as such:
a
0
but also(space)
- Regex are Case sensitive!
- If you want to match special characters like
(),.
etc, escape them using\
. So\(
\.
- If you just want to find any character, use
.
(the wildcard) - If you want to match a Unicode or ASCI special character, use
\xHEX_CODE
- There are options to specify the number of occurrences of a character:
?
for zero or one times that character*
for 0 or more time that character+
for 1 or more times{minimum,maximum}
for a specific number of occurences- if a set of characters is valid, it can be put into a [] and modified together. example:
[0-9]{3,5}
will match all numbers (from 0-9) between 3 and 5 times - important special characters are
^
and$
- they refer to the start of the string (^
) and end ($
) of the string. This allows you to define that the full string can be matched.
Regex are very exact. If just a space or the capitalization is wrong, the string will not match.
Replacing
To replace content with Regex (as necessary for the Replace Action), the best idea is to utilize the so called "group" feature.
By wrapping a part of the RegEx in ()
you define a group.
You can then reference these groups in the "Data" field as $1
for the first group, $2
for the second and so on.
Testing
Regex are notorisouly difficult to write. Getting one right by hand is nearly impossible. We recommend to use regex101 to try your regex before you inpt them on the PG Connect app.
To do, first enter several samples of your target data into the "Test Data" field (each on a new line) Then you can enter the regex into the "regular expression" line at the top. The tool will automatically highlight the matched text and explain the regex on the side.
If you want to test replacing, unfold the "Substition" part and enter the replacement rule in this field.
Examples
Numeric barcode prefixed with B
Regex: B[0-9]+
Matches:
- B1
- B345678
- B3596789491
- AB1DEYFN
- :B1 23
Doesn`t match:
- B 123 (has a space in between)
Numeric barcode prefixed with B - better
Regex: ^B[0-9]+$
Explanation: A 'B' as the first character of the barcode, followed by one or more numbers until the end of the string.
Matches:
- B1
- B345678
- B3596789491
Doesn`t match:
- B 123 (has a space in between)
- AB1DEYFN (doesn't start with a B)
- B1 23 (has extra characters in there)
Cut after 10 digits
Regex (Match): ^([0-9]{10,10}).*
Replacement (Data): $1
Explanation: Matches a barcode that starts with 10 numbers and the anything afterwards (doesn't matter). Replaces with those ten numbers (deleting the rest)
Matches:
- '123456789012335467' -> replaces with 1234567890
- '1234567890' -> replaces with 1234567890
- '1234567890 daosn' -> replaces with 1234567890
Doesn`t match:
- '12345'
- B'1234567890' (doesn't start with a number)
- B1 23 (has extra characters in there)
Reverse Order:
Regex (Match): ([0-9]+)-([a-z0-9]+)
Replacement (Data): $2;$1
Explanation: Matches a Barocde with somer numbers (any length) followed by a '-' followed by an arbitrary length or numbers and (lowercase) characters
Matches: * '1234-ated1234daosn' -> replaces with 'ated1234daosn;1234' * '1234-ated1234 popasmd' -> replaces with 'ated1234;1234'