Skip to main content

Extract order IDs from logs

Extract 0042 and 0187 from log lines, then test the pattern and use it in JavaScript. Matching, unit tests, code generation, and Extraction mode are included in Free. Sign in to save the workspace to your account.

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 first 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 comparison string shown below.
Test stringAssertion targetAssertionComparison string
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 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 the two full matches and their captured digits:

Found match, group 0: ORD-0042
Found match, group 1: 0042
Found match, group 0: ORD-0187
Found match, group 1: 0187

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 searches within records. 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, treat the extraction as an empty list. The current editor can show the original input in that case. 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 Run tests. All five tests pass.
  5. Use Manage Workspace > Edit Title and Tags to give the workspace a name such as Order ID extraction.

A numbered workspace link selects that saved version. Later edits require Update Workspace to create another version.