How to Use RegEx in Remapped Called and Calling Number Mask
Posted by Danny Staub, Last modified by Danny Staub on 15 November 2017 02:55 PM

Regular Expressions:

A regular expression is a sequence of characters that forms a search and replace pattern. This is useful to modify the called or calling number digit strings. The version used in the SmartMedia SN10K units is based on PERL regular expression.

 

The Rule:

The remapped called number mask or remapped calling number mask must have 2 regular expressions, and it must be in the form of '/regexWithCapturingGroup/replaceString/'. In the first regular expression, you specify capturing groups using parentheses (...) while in the second regular expression, you specify how to remap the number by referencing the capturing group using \1, \2, etc.

Please refer to the Quick Reference Table below.

 

Example 1:

If you need to match a called number starts with the digits 450 and then 7 other digits, please put this in the called number mask:

/^450[0-9]{7}$/

^ means the matching is done from the beginning of the number.  $ means the matching is done to the end of then number. 450 is the exact prefix to match. [0-9]{7} means there should be 7 occurrences of the digits 0 to 9. [1-9]{7} would mean 7 digits, excluding 0.

 

Example 2:

In this example, we have to match a called number starts with optional 1, then digits 450 and then 7 other digits.

/^1?450[0-9]{7}$/

? means the preceding pattern, i.e. the digit '1', is optional.

Example 3:

In this example, we have to match a called number with 4 possible prefixes followed by 7 other digits.

/^(800|888|877|866)[0-9]{7}/

In this example, we have 2 independent regular expressions. The first one match prefix '800' with 7 digits, second match prefix '88' with 8 digits.

/^800[0-9]{7}$|^88[0-9]{8}$/

The | (pipe character) means "or"

 

Example 4:

In this example, we have to match a called number with a prefix followed by 6 to 11 digits.

/^880([0-9]{6,11})$/

 

Example 5:

In this example, we want to match any digit string that doesn't start with 080.

/^(?!080)([0-9]*)$/

The ?! is the not operator (everything except this)

 

Example 6:

In the remapped called number mask, the following is specified:

/^(1?)450([0-9]{7})$/\1514\2/

The first regular expression is '^(1?)450([0-9]{7})$'. There are 2 capturing groups. The first one is '(1?)', the optional digit '1'. The second one is '([0-9]{7})', the 7 digits following '450'.

The second regular expression is '\1514\2' which specifies how the remapped number is composed. In this example, let us suppose that the incoming call has the called number as '15141234567'. The digit '1' in the beginning matches the first capturing group '(1?)' and the digits '1234567' match the second capturing group '([0-9]{7})'. Therefore, the remapped called number would be '1' followed by '514' followed by '1234567', that is '15141234567'.

 

Example 7:

This is a simpler example in which the remapped called number mask would remove the prefix '852' while keeping the 8 digits following the prefix.

/^852([0-9]{8})$/\1/

First group between parenthesis are the 8 digits after the 852. The output number will be the 8 digits after 852 (represented by the first group "\1" used in the seconds section of the regex)

 

 

Quick Reference Table:

 

Regular Expression Pattern

Explanations

Examples

Meta characters  [\^$.|?*+(

Special characters used in regex.

  • Must be escape with backslash "\" to use a literal characters.

Literal characters

All characters (except the metacharacters) match a single instance of themselves. 

  • { and } are literal characters, unless they're part of a valid regular expression token (e.g. the {n} quantifier).

/a/ matches "a"

[characters]

Character classes or character set. A character class matches a single character out of all the possibilities offered by the character class.

/[0-9]/ matches a single digit

[\d]

Shorthand character classes matching digits. Same as [0-9].

/[\d]/ matches a single digit

.

Dot matches any characters.

/a.c/ matches both "a4c" and "ayc"

^

Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character.

$

Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character.

{m,n}

Matches at least “m” and at most “n” occurrences of preceding character, character class or group.

*

Matches zero or more occurrences of preceding character, character class or group.

+

Matches one or more occurrences of preceding character, character class or group.

 ?

Matches zero or one occurrences of  preceding character, character class or group.

()

Parentheses are used for group or capturing group

\0, \1, \2, ...

Substitute the value matched by the nth grouped sub-expression, used in remapped fields.

 ?!

Not, as in "everything except this".

 

 

 

Here are some examples:

 

Add 2720 prefix:

 

/^(\d+)$/2720\1/

 

or

 

/^([0-9]*)$/2720\1/

 

 

Strip first 4 digits:

 

/^([0-9]{4})([0-9]*)$/\2/

 

 

Strip # and 7 first digits:

 

/^([#])([0-9]{7})([0-9]*)$/\3/

 

 

 

(8748 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).