Skip to main content

Extract order IDs from logs

Extract order IDs from logs, check the pattern with unit tests, and use it in JavaScript.

Find the identifiers​

  1. Select JavaScript and Match.
  2. Enable g and remove the other flags.
  3. Paste these lines into Test String:
ORD-0042 pending
ORD-0187 sent
ORD-42 invalid
XORD-0001 invalid
  1. Enter ORD-[0-9]+ in Regular Expression.

This pattern finds four matches. It includes the short ID and the ORD-0001 text inside XORD-0001. Replace it with:

\bORD-(?<id>[0-9]{4})\b

Open in regex101

The result is two matches:

Full matchCapture group id
ORD-00420042
ORD-01870187

{4} requires four digits. The word boundaries prevent matches beside another word character. The capture group stores the digits without the prefix.

Add regression tests​

  1. Switch to Unit Tests. Keep the same pattern and g flag.
  2. Select Add unit test for each row.
  3. Enter the test string, assertion target, assertion, and expected value shown below.
Test stringAssertion targetAssertionExpected value
ORD-0042 pendingcapture group 1 (id)equals0042
ORD-0187 sentcapture group 1 (id)equals0187
ORD-42 invalidregexdoes not matchNot used
XORD-0001 invalidregexdoes not matchNot used
Empty stringregexdoes not matchNot used
  1. Select Unit test actions > Run tests. All five tests pass.
  2. Change {4} to {2,4} and run the tests again. The ORD-42 test fails.
  3. Restore {4} and run the tests. All five pass again.

The unit-test reference describes the other assertions. Capture comparisons inspect the first match in each test string.

Run the generated JavaScript​

  1. Switch back to Match. The four-line input remains in Test String.
  2. Open Code Generator from the tools menu.
  3. Set Language to JavaScript.
  4. Select Copy code and save the code as extract-orders.mjs.
  5. Run node extract-orders.mjs.

The generated loop prints each full match and its captures, including the named id group.

To return an array of IDs instead of printing each group, use:

const input = 'ORD-0042 pending\nORD-0187 sent\nORD-42 invalid\nXORD-0001 invalid'
const ids = [...input.matchAll(/\bORD-(?<id>[0-9]{4})\b/g)].map((match) => match.groups.id)
console.log(ids) // ['0042', '0187']

This pattern finds IDs within text. For an identifier field that must contain nothing else, use the whole-input pattern.

Copy a newline-separated list​

  1. Switch to Extraction.
  2. Enter $1\n as the replacement.
  3. Verify that the match count is 2.

The output is 0042\n0187\n: one ID per line, with a newline after the last ID. Extraction mode removes the status text from this matching input.

If the match count is 0, the extraction output is an empty string. For JSON or CSV match data, use Export Matches.

Save the pattern and tests​

  1. Sign in, then open Save & Share > Save Workspace.
  2. Copy the Access Link from the save dialog.
  3. Reopen the link. It restores the pattern, input, Extraction template, and five unit tests.
  4. Switch to Unit Tests and select Unit test actions > Run tests. All five tests pass.
  5. Use Manage Workspace > Add personal title and tags to name it Order ID extraction.

A numbered workspace link opens that saved version. Use Update Workspace to save later edits as another version.