Knowledge Base

How to use regular expressions in CodeTwo Email Signatures 365

It’s common to have inconsistent data across different users in Entra ID or Active Directory. By default, CodeTwo Email Signatures 365 pulls this data from your user directory and displays it in email signatures as is – without changing the values. However, by using regular expression formatting, you can fully control how user information, such as phone numbers, appears in email signatures – without needing to edit or standardize user attributes in Entra ID.

Watch the video below to see how it works or continue reading for more info and examples on using regular expressions in CodeTwo Email Signatures 365. 

What is a regular expression?

A regular expression, or regex, is an advanced pattern used to identify (and optionally replace) specific phrases or sequences of characters (strings). For example:

  • . (the dot) means any character in a regular expression.
  • \d or [0-9] means any single digit.
  • [a-zA-Z] means any single character (both lowercase and uppercase).

For more basic examples of regex, see this article.

How to format user information with regex

CodeTwo uses placeholders to add user info and contact details to email signatures and automatic replies. By using regex formatting, you can control exactly how this data appears in signatures without needing to update anything in Entra ID. This is especially useful if:

  • You need to keep only numerical values (like phone numbers) in Entra ID, but want them to appear in a more readable format (e.g., with spaces or hyphens) in email signatures.
  • The data stored in Entra ID is inconsistent (e.g., letter capitalization, different format of phone numbers), and you want to standardize it in an easy and quick manner.
  • You want to change the format of certain data but don’t have permissions to update Entra ID.
  • You need to show or hide certain information (e.g. extension from a phone number) in your email signatures.

You can adjust the formatting for placeholders in the signature template editor. First, add the placeholder to your template, then right-click it and select Edit placeholder (alternatively, click the placeholder added to the template, go to the Placeholder tab and then click Edit). Next, in the Edit placeholder window, click Find & replace values (Fig. 1.).

The formatting options are not available for some placeholders. Learn more

Accessing the regex formatting setting for a placeholder.
Fig. 1. Accessing the regex formatting setting for a placeholder.

This lets you access the Find and Replace with fields (Fig. 2.). Enter a pattern (a regular expression) in the Find field to match specific sequences of characters (e.g. phone numbers). If this pattern is found in the Entra ID attribute, it will be replaced or formatted according to the pattern provided in the Replace with field – see example shown in Fig. 2.

If you’re familiar with regex, the (a)+ button next to the Find / Replace with field (see Fig. 2.) contains a cheat sheet with some of the most common regular expression patterns. Once you select a pattern, it will be automatically inserted into the appropriate field.

If you’re new to regex, here’s a helpful article that explains the basics. For now, you can read on, as we provide some ready-to-use patterns that you can use without needing to fully understand regex.

1093-2-regex-example
Fig. 2. An example of a regular expression.

To test your regex pattern, click Test (see Fig. 2.). This allows you to check how certain values will be formatted in real time (Fig. 3.). If everything looks good, close the Test formatting window and then click OK to save your regex formatting pattern.

It’s also a good idea to use the signature preview feature to check how email signatures will look for specific users once you apply this change.

Testing regular expressions.
Fig. 3. Testing regular expressions.

Ready-to-use regexes & examples of use

Each example includes a brief explanation of how the regex pattern works and what to enter in the Find and Replace with fields in the signature template editor (see Fig. 2.).

Formatting phone numbers

Formatting phone numbers is one of the most common uses for regex in email signatures. Below are some examples of how regex formatting can help you unify phone numbers in different ways.

  1. Change any phone number format to standard US format: (123) 456-7890
  2. Change phone number format from 1234567890 to 123.456.7890
  3. Remove the country code from a phone number
  4. Change phone number format from 123.456.7890 to 1234567890
  5. Add the country code to phone numbers in external emails
  6. Remove a prefix
  7. Apply custom formatting

1. Change any phone number format to standard US format: (123) 456-7890

This regex finds and removes the country code (if it’s there), takes only the numbers and formats them in the standard US phone number format. It also removes any extensions from the end of the string. It works with various phone number formats, including those with spaces, dots, hyphens, or parentheses. For example, it converts numbers like +11234567890, 1234567890;ext:=1234, +1-123-456-7890 or (+1).123.456.7890 into (123) 456-7890

  • Find:
(?:\D*(?:\+|00)1|0\D*)?\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)(?:\D*(?:ext|extension|x|;|=)\D*\d*)?
  • Replace with:
($1$2$3) $4$5$6-$7$8$9$10

This regex is designed to work only with US phone numbers that consist of a 3-digit area code followed by a 7-digit local number. If the phone numbers start with a leading 0 or the US country code (001 or +1), these will be removed by the regex.

2. Change phone number format from 1234567890 to 123.456.7890

Similar to the previous example, but uses simpler syntax. This pattern only works if all numbers are in the same format, with no spaces or special characters. So, for example, it changes 1234567890 into 123.456.7890.

  • Find:
(\d{3})(\d{3})(\d{4})
  • Replace with:
$1.$2.$3

If you prefer to use another special character in place of dots, simply replace them with the character of your choice, such as a hyphen (-) or a space.

3. Remove the country code from a phone number

This pattern only removes the country code without applying additional formatting to the phone number. You can replace the country codes in the example with your own. For example, this regex will change +1(123)456-7890 to (123)456-7890.

  • Find:
(\+49|\+33|\+1)

Here's a more complex syntax that works with a wider range of phone number formats (that include spaces, dots, hyphens, or the country code in 00x format): 

\(?\+1|001\)?[\s.-]*|\(?\+33|0033\)?[\s.-]*|\(?\+49|0049\)?[\s.-]*
  • Replace with:
(leave blank)

4. Change phone number format from 123.456.7890 to 1234567890

This regex pattern strips away any formatting, leaving only numeric values. For example, it changes 123-456-7890 or 123.456.7890 or (12) 34 56-7890 to 1234567890.

  • Find:
[^0-9]
  • Replace with:
<leave blank>

5. Add the country code to phone numbers in external emails

If you’re happy with the formatting but want to add your country code for some emails, the regex below adds the country code. For example, it changes (123) 456-7890 into +1 (123) 456-7890.

  • Find:
^(\d*)
  • Replace with:
+1 $1

Remember to replace +1 with your country code if it’s different. If you don't want to include a space after the country code, simply remove it.

6. Remove a prefix

Some phone numbers have prefixes that need to be stored in Entra ID but removed from email signatures. For example, if the phone number format is x0000; 1234567890, this regex will remove the prefix.

  • Find:
(x.{5})?(\d?)(\d?)(\d?)(\d?)(\d?)(\d?)(\d?)(\d?)(\d?)(\d?)(\d?)
  • Replace with:
$2$3$4$5 $6$7$8$9$10$11

7. Apply custom formatting

You can create custom formats for phone numbers. For example, this regex changes 491234567890 to +49(0)1234/567-890.

  • Find:
(\d{2})(\d{4})(\d{3})(\d{3})
  • Replace with
+$1(0)$2/$3-$4

Formatting or modifying email addresses

Regex can be used to modify how email addresses appear in your email signatures. For example, you can capitalize some letters (changing [email protected] to [email protected]) or replace the domain with a custom one. Keep in mind that if you change the domain to an inactive one will prevent emails from being delivered when someone clicks on the custom email address.

The following regex finds the @ symbol and all characters after it, then replaces those elements with the string (domain) of your choice. For example, this will change both [email protected] and [email protected] to [email protected] and [email protected]

  • Find:
@.*
  • Replace with:
@YourCustomDomain.com

This regex will shorten the username part of an email address by removing the dot (.) and everything after it. For example, it will change [email protected] to [email protected]

  • Find:
(?=\.)(.*?)(?=\@)
  • Replace with:
<leave blank>

If you only want to change a specific domain (e.g. @company.com to @mycompany.com), there's no need to use regex. Simply enter the old domain in the Find field and the new domain in the Replace with field, like this:

  • Find:
@company.com
  • Replace with:
@mycompany.com

FAQ

  1. How does regex formatting work with RT tags?
  2. Can I format a single placeholder value in more than one way?
  3. Will the signature preview show regex formatted values?
  4. Can I use regex formatting together with basic formatting?
  5. Can I use regex formatting to replace empty values?

How does regex formatting work with RT tags?

RT tags (the Remove empty placeholders feature) are used to remove placeholders, along with any surrounding text or elements, from email signatures if the placeholders are replaced with blank values (because certain information is missing for a user in Entra ID). For example, if a phone number isn’t available for a user, RT tags can remove the phone number and any related text (e.g. Phone:) from the signature.

If a placeholder value is removed using regex formatting, RT tags will behave the same way as if the value were missing in Entra ID.

Learn more about RT tags

Can I format a single placeholder value in more than one way?

Regex formatting is applied per placeholder per signature template. This means you can apply different formatting for the same placeholder in different signature or autoresponder rules.

For example, you could add a country code to the {Phone} placeholder only in signatures added for external emails or for recipients in other countries, and leave a simplified phone number format for internal communication.

On the other hand, if you use the same placeholder multiple times in the same email signature (e.g., the {Phone} placeholder is used as text in the contact details section and as a callto: link in another part of the signature), the regex formatting will apply to both instances of the placeholder.

Will the signature preview show regex formatted values?

Yes. The preview feature lets you see how your email signature will look for different users in actual emails. It’s a good idea to check the preview for various users before applying regex formatting – just to make sure it works as expected.

See how signature preview works

Can I use regex formatting together with basic formatting?

Yes. Basic formatting is applied after the regex formatting. So, for example, if your regex formatting results in “security officer”, you can use the Capitalize each word option (see Fig. 4.) to change it into “Security Officer”.

Using basic placeholder formatting.
Fig. 4. Using basic placeholder formatting.

Can I use regex formatting to replace empty values?

Currently, you cannot use regex in CodeTwo Email Signatures 365 to replace empty user attributes with default values. However, you can achieve this by using conditional placeholders.

Was this information useful?