Toolpack: How to Use RegEx in Remapped Called and Calling Number Mask
Contents |
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 Commonly Used Regular Expression Pattern for the patterns usually used.
Example 1
In the remapped called number mask, the following is specified:
/^(1?)450([0-9]{7})$/\1514\2/
The first regular expression, regexWithCapturingGroup, 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, replaceString, is '\1514\2' which specifies how the remapped number is composed. The rule is that the digits in the called number that match the expressions in the capturing groups would be substituted in replaceString.
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 2
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/
Example 3
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 - 9.
Example 4
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 5
In this example, we have to match a called number with 4 possible prefixs followed by 7 other digits.
/^(800|888|877|866)[0-9]{7}/
Caution
It is recommended to use ^ and $ to specify the matching of beginning and end of the number if it is appropriate so as to avoid ambiguity in matching. For example, if you specify the mask as /450[0-9]{7}/, the following numbers would match this mask and that may not be desirable.
- 864501234567 (the later part of the number matches the mask)
- 4501234567890 (more than 7 digits after '450' is still a match)