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
- Select JavaScript and Match.
- Enable
gand remove the other flags. - Paste these lines into Test String:
ORD-0042 pending
ORD-0187 sent
ORD-42 invalid
XORD-0001 invalid
- 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
The result is two matches:
| Full match | Capture group id |
|---|---|
ORD-0042 | 0042 |
ORD-0187 | 0187 |
{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
- Switch to Unit Tests. Keep the same pattern and
gflag. - Select Add unit test for each row.
- Enter the test string, assertion target, assertion, and comparison string shown below.
| Test string | Assertion target | Assertion | Comparison string |
|---|---|---|---|
ORD-0042 pending | capture group 1 (id) | equals | 0042 |
ORD-0187 sent | capture group 1 (id) | equals | 0187 |
ORD-42 invalid | regex | does not match | Not used |
XORD-0001 invalid | regex | does not match | Not used |
| Empty string | regex | does not match | Not used |
- Select Run tests. All five tests pass.
- Change
{4}to{2,4}and run the tests again. TheORD-42test fails. - 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
- Switch back to Match. The four-line input remains in Test String.
- Open Code Generator from the tools menu.
- Set Language to JavaScript.
- Select Copy code and save the code as
extract-orders.mjs. - 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
- Switch to Extraction.
- Enter
$1\nas the replacement. - 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
- Sign in, then open Save & Share > Save Workspace.
- Copy the Access Link from the save dialog.
- Reopen the link. It restores the pattern, input, Extraction template, and five unit tests.
- Switch to Unit Tests and select Run tests. All five tests pass.
- 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.