Toolpack: How to Use RegEx in Remapped Called and Calling Number Mask

From TBwiki
Jump to: navigation, search

Contents

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 TelcoBridges 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 Commonly Used Regular Expression Pattern for the patterns usually used.

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 expression. 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)

SIP specific matching

Matching the complete SIP address

When matching a field like 'called', 'calling' or 'private_address' for a SIP call, extra parameters are available.

A typical SIP address is formatted like so: user@host[:port], with the port section optional.

When applying the regex, we first try the `user` substring first, then we try to match the whole SIP address.

For example, consider a route with the following regex:

/alice@telcobridge.com/

It will only match if both the user is 'alice' and the domain is 'telcobridge.com'

Remapping the complete SIP address

In a similar way, we can apply a '/regexWithCapturingGroup/replaceString/' regex to cover the complete SIP address. Just as in the matching section, we try to apply the regex on the 'user' string first, then in case of failure we try with the complete SIP address.

The resulting remapping must be in the 'user@host[:port]' format, since the replacing string is taken apart to fill each parameter.


Related Links

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)
Personal tools