Autoreplacement for phone numbers
Last updated
Last updated
There are many possible ways to write the same phone number. 81234567890
, +71234567890
, 1234567890
and 8 (123) 456-78-90
are only a few obvious formatting examples. If you are getting calls from multiple countries, the number of options may be very high.
For a human, those options are not very different. All of them will work if you dial them on the phone; it's easy to compare them and understand that they are the same number. However, for software, they are completely different numbers. For example, if you have a contact saved in CRM with a number starting with +7, but you type the same number in the search with 8 instead, then the search may not show this contact.
We used to only have number formatting in customizations to solve this problem. Now we've added an autoreplacement function in Itgrix bx v3.7.0 and Itgrix amo v2.10.0. It allows to configure number formatting using regular expressions (regexp) right from the admin panel.
Autoreplacement modifies client and/or user phone numbers that match templates by replacing whole numbers or their parts with substitutions. Multiple replacement patterns can be configured simultaneously.
Replacements are executed before requests are sent to CRM, so autoreplacement affects the creation and search of entities, tasks, and users.
All replacements are executed for each number one by one in turns. It means that the order of replacements is important, as the execution of the first replacement may make the number match or not match the following templates.
We use the language of regular expressions (regexp) to describe the rules of autoreplacement, the same as we used to do before in customizations. This is a commonly used way of matching and modifying text.
To create and test regular expressions, we suggest using online tools, such as regex101.com (choose Golang in the left menu and lower choose Substitution to test replacements).
Symbols
^
- beginning of a line
$
- end of a line
.
- any symbol
\d
- a digit
\D
- not a digit
\w
- a digit or a letter
\W
- not a digit and not a letter
\s
- space (including unusual varieties, such as no-line-break spaces)
\S
- not a space
[ab\s0-5]
- either: a
, b
, space or digit between 0
and 5
abc 123
- literally "abc 123", possibly as a part of bigger text fragment
abc|123
- either "abc" or "123", possibly as a part of bigger text fragment
^abc 123$
- "abc 123" as a whole line without additional text before or after
\(
, \)
, \[
, \]
, \{
, \}
, \+
, \?
, \*
, \.
, \|
, \\
- literal representation of symbols that have special meaninng when not preceeded by \
Repetitions
You can specify how many times a symbol or a group can be repeated by adding of the the following immediately after.
?
- 0 or 1 time
+
- 1 or more times
*
- any number of times (even zero)
{5}
- 5 times
{5,10}
- 5 to 10 times
{,10}
- 0 to 10 times
{5,}
- 5 or more times
For example, \d{10}
means "ten digits", \+?
means "plus sign, zero or one time" and 0+
means "one or more zeroes".
Groups and substitutions
Symbols can be combined into groups using brackets. This is useful for repeating multiple symbols. For example, (10){3}
means "101010".
Text from groups can be used in substitutions as $N
where N
is the number of a group.
For example, when using a pattern 8(\d{6})(\d{4})
, the substitution $1
would mean the contents of the group (\d{6})
- the first 6 digits after "8". The substitution $2
would mean the contents of the group (\d{4})
- the following 4 digits.
Pattern | Substitution | Description |
---|---|---|
Pattern | Substitution | Description |
---|---|---|
^\+?7(\d{10})$
8$1
Replaces prefixes +7
and 7
in the beginning of a number that consists of a prefix followed by 10 digits with 8
, e.g. +74951234567
→ 84951234567
and 74951234567
→ 84951234567
This pattern is enabled by default
^\+7
8
Replaces +7
in the beginning of any number with 8
, e.g. +712345671234567
→ 812345671234567
.*(\d{10})$
8$1
Replaces a number with 10 in the end with 8
and those 10 digits, e.g. 0071234567890
→ 81234567890
^(\d{7})$
8495$1
Adds a prefix 8495
to a 7-digit number, e.g. 1234567
→ 84951234567
\D
(empty string)
Removes all symbold except digits, e.g. +7 (495) 123-45-67
→ 74951234567
[\s()-]
(empty string)
Removes all spaces, brackets and dashes, e.g. +7 (495) 123-45-67
→ +74951234567
^(.*)(\d{3})(\d{3})(\d{2})(\d{2})$
$1 ($2) $3-$4-$5
Formats a number as x (xxx) xxx-xx-xx
, e.g. +74951234567
→ +7 (495) 123-45-67
^0\d{2}(\d+)$
8$1
Replaces a number composed of "zero, two digits, more digits" with 8
and the digits from the end of a number, e.g. 0771234567890123
→ 81234567890123
1234567$
7654321
Replaces 1234567
in the end of a number with 7654321
, e.g. 84951234567
→ 84957654321
^84951234567$
84957654321
Replaces a literal number 84951234567
with 84957654321
.*(\d{4}$)
$1
Removes everything except the last 4 digits, e.g. +71234564321
→ 4321
^8123456(\d{4})$
$1
Removes a prefix 8123456
, leaving last 4 digits, e.g. 849576543214321
→ 4321
.*123456(\d{4})$
$1
Removes everything except the last 4 digits from a number ending with 123456
and 4 more digits, e.g. +71234564321
→ 4321
^1\d{3}$
2$1
Replaces the first digit 1
of a 4-digit number with 2
, e.g. 1234
→ 2234
^1234$
4321
Replaces literal number 1234
with 4321