How to extract a phone number from text with a regular expression

A practical example: find a phone number in a text value, choose a regex preset, keep the matched number, and normalize it.

A supplier can send a phone number as part of a longer value, for example Phone: +1 (415) 555-0198 ext. 24. In value settings, you can find the number with a regular expression, keep the matched fragment, and then normalize it.

The rule is available in value transformations for XML, JSON, and table sources. You can type the regular expression manually or choose a preset with the star button next to the pattern field.

Choose a regular expression from presets

For phone extraction, use the regular expression condition and the remove all except action. This action keeps the full matched fragment and removes the text around it.

  1. Open the value settings for the phone field.
  2. Add a rule and select the regular expression condition.
  3. Click the star button and choose a phone preset.
  4. Select remove all except as the action and save the rule.
Animation showing how to choose a phone regular expression from Eofferix presets
The preset inserts the pattern into the regular expression field; you can still edit it manually.

Option 1: keep the matched phone number

Use this when you need to remove text around the phone number but keep the number formatting from the source.

BeforeRuleAfter
Contact: +1 (415) 555-0198 ext. 24\+?\d[\d\s().-]{8,}\d + remove all except+1 (415) 555-0198
phone 1 212 555 0104\+?\d[\d\s().-]{8,}\d + remove all except1 212 555 0104

Option 2: normalize the number

If you need one leading plus sign, use a three-rule sequence: first keep the matched phone number, then remove every non-digit character with the regular expression \D+, and then add + at the beginning when the current value is not empty.

Sequential phone cleanup rules in Eofferix value settings
The second rule removes every non-digit character with a regular expression; the third rule adds a plus sign to a non-empty value.
BeforeSequenceAfter
Phone: +1 (415) 555-0198 ext. 24keep phone → remove \D+ → if the current value is not empty, add ++14155550198
1 212 555 0104keep phone → remove \D+ → if the current value is not empty, add ++12125550104

Important details

  • remove all except keeps the first full match. It does not collect every digit one by one.
  • To normalize the number to digits, use the regular expression \D+ with the remove substring action.
  • If you need country-code-specific normalization, add that rule before the final + step.