Transformations

How to extract a barcode from text with a regular expression

2026-05-27
Task

A source value contains a label and an expected contiguous candidate of 8 or 12–14 ASCII digits. Keep it as text, preserve a leading zero, and do not extract a fragment from a longer digit run.

Short answer

In Transformations, add two rules in this order: first Not a regular expression with the guard pattern → Do not export field, then Regular expression with the extraction pattern → Remove everything except. The first rule clears an unsuitable value; the second keeps the first matching candidate.

How to do it in Eofferix

  1. Open the settings of the output column or node. The name barcode is only an example; your field may have any other name. Keep the result as text so a leading zero is preserved.

  2. In Transformations, add the guard rule: Current valueNot a regular expression with (^|[^0-9])(?:[0-9]{12,14}|[0-9]{8})([^0-9]|$)Do not export field.

  3. Immediately after it, add the extraction rule: Current valueRegular expression with (?:[0-9]{12,14}|[0-9]{8})Remove everything except. Do not reverse the two rules.

    Two rules for safely extracting a barcode candidate in Eofferix
    The upper rule clears a value without an allowed candidate; the lower rule keeps the first candidate of an allowed length.
  4. Apply the rules and check the examples below in the preview. Then run a small test export and open the resulting file: unsuitable values should be empty, and suitable values should equal the first candidate found.

Expected result

Source valueResultWhy
EAN: 40063813339314006381333931A separate 13-digit candidate is found.
barcode 012345678905012345678905The 12 digits stay as text, including the leading zero.
code 1234567812345678An 8-digit candidate is allowed.
1234567890123412345678901234A 14-digit candidate is allowed.
123456789012345emptyThe recipe must not extract 14 digits from a longer digit run.
ABC-12X34emptyThere is no contiguous candidate of an allowed length.
first 12345678 second 400638133393112345678When several candidates match, the first one is kept.

What to check before export

  • Both patterns use ASCII digits [0-9] and allow only 8 or 12–14 digits.
  • The candidate digits must be contiguous. If a supplier inserts spaces or hyphens inside the code, add a separate normalization step and retest the boundary cases.
  • When several candidates match, Remove everything except keeps the first one.
  • This recipe checks only format and length. It does not verify an EAN/UPC/GTIN check digit or confirm that a product with the code exists.

Process data faster with Eofferix

Create a free account to automate catalogs and price lists around your own rules.

Sign up