Pattern and Replacement RegEx

Updated 3 months ago by Kevin

Overview

  • Locations within ShipStream that use RegEx(regular expression) patterns
  • Learn how to make a RegEx pattern for the field Lot Number Input with the Extract Date selected and a replacement pattern ( The RegEx pattern instructions are applicable to all of ShipStream's RegEx fields )
  • Tips on using basic RegEx components and some commonly used patterns
  • External Resource for testing RegEx
ShipStream uses PCRE compliant RegEx.

Locations that use RegEx Patterns:

  • Catalog > Lot Types:
    • Lot Number Pattern
    • Lot Number Replacement
    • Expiration Date Pattern
    • Expiration Date Replacement
  • Catalog > Regulations:
    • Technical Name Pattern
    • Additional Regulatory Information Pattern
    • SKU Pattern
  • System > Plugins > Subscriptions:
    • Shipping Method Translation
  • Shipping > Packaging Features — Data Collection > Packaging tab:
    • Collect Data Pattern
  • System > Configuration > Catalog > Catalog > General:
    • Not-Regulated Goods SKU Pattern
    • ORM-D Goods SKU Pattern
    • Limited Quantities: Consumer Commodities SKU Pattern
    • Dangerous Goods/Hazardous Materials SKU Pattern
  • System > Configuration > Warehouse > Scanner > Display:
    • Mobile User Agent
  • System > Configuration > Warehouse > Stock > Locations:
    • Pickable Location Patterns*
    • Pallet Location Patterns*
* Does not support none bracketed Type classes

Lot Types

The Lot Number

The example lot number is

AB190405C
Lot Number Breakdown
  • Two capital letters —  AB
  • Six numbers that represent a date, in this case it is the year 19, month 04, and day 05.
  • One capital letter —  C
The RegEx Pattern

This RegEx pattern can read the lot number and extract out the date info from within the lot number.

^[A-Z]{2}(?'year'\d\d)(?'month'\d\d)(?'day'\d\d)[A-Z]$
RegEx Pattern Breakdown
  • ^ — The first character must be the first in line
  • [A-Z] — A capital letter from 'A' to 'Z'
    • {2} — Match two capital letters
  • (?'year' — Start a named capture group called 'year'
    • \d — A numeric digit from 0 to 9; match one
    • \d — Another numeric digit; match one
    • ) — Close the 'year' capture group
  • (?'month'\d\d) — A capture group named 'month' that matches two numeric digits
  • (?'day'\d\d) — A capture group named 'day' that matches two numeric digits
  • [A-Z] — A capital letter; match one
  • $ — the last character to be matched must be the last in line

Summarizing the total Pattern:

 The pattern is looking for consecutive characters that are:

  • Two capital letters — not captured
  • Two digits — captured for the 'year'
  • Two digits — captured for the 'month'
  • Two digits — captured for the 'day'
  • One capital letter — not captured
During Processing if the lot number does not match this pattern then it will be rejected and an error will be shown to the user.
The Replacement Pattern

To craft a Replacement pattern use the capture groups from the RegEx pattern above. A named capture group can be called by using a dollar sign and braces. The Replacement pattern is:

${year}-${month}-${day}
Replacement Pattern Breakdown
  • ${year} — Place the characters captured by 'year' here
  • - — Place a dash
  • ${month} — Place the characters captured by 'month' here
  • - — Place a dash
  • ${day} — Place the characters captured by 'day' here

Using the example lot number the output would be 19-04-05

The capture groups can be rearranged as needed. An example would be to call the month, day, and then the year.  You can also change the separators, an example would be to use the forward slash / to separate the numbers.  Example: ${month}/${day}/${year} would have output like this: 04/05/19 .​

Tips

Common Capture patterns
  1. To capture any input entered use (.+)
    • ( — Starts an un-named capture group
    • . — match any character
      • + — Match 1 or more characters
    • ) — close the capture group
    • To use the captured character in a Replacement pattern use $1 which is calling capture group 1. Since it was un-named it gets a default number name
  2. To capture a numeric lot number that should at least be 7 digits long use (\d{7,})
    • ( — Starts an un-named capture group
    • \d — A numeric digit from 0 to 9
      • {7,} — Match 7 or more characters
    • ) — close the capture group
Type Classes

Type Classes are how you specify what characters you want to match.

  • To match "abc" use abc
  • To match any single character use a period .
  • To match capital letters only use [A-Z]
    • [ — Start a character class — any character matching a character within the brackets will be excepted
    • A-Z — Match any character between capital 'A' through capital 'Z', inclusive
    • ] — End character class
  • To match a digit use \d alternatively you can use [0-9]
  • To match any word character use \w this is equivalent to using [a-zA-Z0-9_]
    • This matches any upper or lowercase letter, 0 through 9, and underscore '_'
  • [A-Z0-9_-] — To match Capital letters, 0 - 9, underscore '_', and dash '-'
Quantifiers

Quantifiers are how many times to match a character or a pattern.
If the quantifier is after a character or a type class then the quantifier will match that character type that many times.

  • To match a single character type that letter; to match the letter 'r' use r
  • To match one or more "r"'s use r+
  • To match three "r"'s use r{3}
  • To match three or more "r"'s use r{3,}
  • To match three to seven "r"'s use r{3,7}
Capture Groups

Capture Groups are how you save the matched items for later use.

  • Named Capture groups
    • (?'name'...)(?'year'\d{4}) will capture 4 consecutive digits
      • Recall capture group using ${name}${year}
  • Single Capture Group
    • (\d) — Recall the capture group using $1
  • Multiple Capture groups
    • (?'MFR'[A-Z]{3})-(?'lot'[A-Z0-9_-]{7,})
      • Recall using ${MFR} and ${lot}
      • Lot #:${lot}; Manufacturing:${MFR}
    • Alternatively you could have made the capture pattern like this ([A-Z]{3})-([A-Z0-9_-]{7,})
      • Recall the groups using $1 and $2
      • Lot #:$2; Manufacturing:$1

Resources for RegEx Patterns

  • Use Regex101 to test your patterns against example Lot Numbers. Also learn more advanced RegEx from the Quick Reference section in the lower right corner. Regex101 can make it easy to test patterns and find out what is being captured and what is not.  Try using the above lot number and pattern at that website.  You can even play around with the Replacement pattern in the Substitution section at Regex101.
  • Another source for RegEx info is RexEgg regex quick-start guide — though it has more depth then is needed to use RegEx inside of ShipStream.
  • Ask for help from ShipStream


How did we do?