Using regex to create powerful and flexible call routing rules in session border controllers

Last Updated on January 9, 2023 by Jaron Davis

Please note

This article has been written by AI and may contain inaccuracies.

Introduction

Regular expressions, or regex for short, are a powerful tool used in many fields, including computer science, data analysis, and even in session border controllers (SBCs). Regex allows users to create patterns that can be used to match, search, and manipulate strings of text. In the context of SBCs, regex is used to create rules for routing calls based on certain patterns in the caller’s information.

In this article, we will cover the basics of regex and its common use in SBCs. We will also provide a brief tutorial on some of the most commonly used regex concepts, and include visual aids such as tables to help illustrate these concepts.

What is Regex?

Regex is a language that is used to create patterns that can be used to match or manipulate strings of text. These patterns are created using a combination of characters and symbols, and can be used to perform tasks such as searching for specific patterns in a block of text, replacing certain words or phrases with new ones, and even extracting specific pieces of information from a larger string.

Regex is used in many different programming languages and tools, and is especially useful for tasks that involve working with large amounts of text data. For example, a web developer might use regex to extract links from a webpage, or a data analyst might use regex to clean up a large dataset by removing any rows that contain certain patterns of data.

Regex in Session Border Controllers

Session border controllers, or SBCs, are networking devices that are used to manage and control the flow of voice and video traffic in Voice over Internet Protocol (VoIP) networks. One of the primary functions of an SBC is to route calls based on certain patterns in the caller’s information.

In order to do this, SBCs use regex to create rules that dictate how calls should be routed based on certain patterns in the caller’s information. For example, an SBC might have a rule that routes all calls from a certain area code to a specific location, or a rule that routes calls from a certain phone number to a voicemail system.

Creating Regex Rules in SBCs

The process of creating regex rules in an SBC typically involves defining a pattern that the SBC should look for in the caller’s information, and then specifying what action the SBC should take if the pattern is found.

For example, let’s say that we want to create a rule in an SBC that routes all calls from a certain area code to a specific location. To do this, we would first define the pattern that we want the SBC to look for. In this case, the pattern would be the area code that we want to match.

Next, we would specify the action that the SBC should take if the pattern is found. In this case, the action would be to route the call to the specific location.

Regex Tutorial

Now that we’ve covered the basics of regex and how it is used in SBCs, let’s take a look at some of the most commonly used regex concepts.

  1. Anchors:

Anchors are special characters that are used to match the position of a character in a string, rather than the character itself. There are two common anchors that are used in regex: the “^” character, which matches the beginning of a string, and the “$” character, which matches the end of a string.

For example, the pattern “^A” would match any string that begins with the letter “A”, while the pattern “B$” would match any string that ends with the letter “B”.

  1. Wildcards:

Wildcard characters are used to match any character in a string. The most common wildcard character is the “.” character, which matches any single character.

For example, the pattern “A.” would match any string that begins with the letter “A” and is followed by any single character. This could include strings such as “A1”, “A2”, “A3”, etc.

  1. Character classes:

Character classes are used to match any character that belongs to a certain set. Character classes are denoted using square brackets, and can include any combination of characters or ranges of characters.

For example, the pattern “[0123]” would match any string that contains any of the digits 0, 1, 2, or 3. The pattern “[A-Z]” would match any string that contains any uppercase letter.

  1. Grouping and alternation:

Grouping is used to group together multiple characters or patterns, and is denoted using parentheses. Alternation is used to match one of several different patterns, and is denoted using the “|” character.

For example, the pattern “A(B|C)” would match any string that begins with the letter “A” and is followed by either the letter “B” or the letter “C”.

  1. Repetition:

Repetition is used to match a character or pattern multiple times. There are several different ways to specify repetition in regex, including the “*” character, which matches zero or more repetitions of the preceding character or pattern, the “+” character, which matches one or more repetitions, and the “?” character, which matches zero or one repetition.

For example, the pattern “A*” would match any string that begins with zero or more “A” characters, while the pattern “A+” would match any string that begins with one or more “A” characters.

  1. Special characters:

There are several special characters in regex that have specific meanings, and must be escaped using a backslash if they are to be treated as literal characters. Some common special characters include the “^” character, the “$” character, the “.” character, the “*” character, the “+” character, the “?” character, the “|” character, and the “” character.

Examples of Regex in an SBC

Example 1:

In this example, we want to create a rule in an SBC that routes all calls from a certain area code to a specific location.

To do this, we would first define the pattern that we want the SBC to look for. In this case, the pattern would be the area code that we want to match. Let’s say the area code we want to match is “212”.

Next, we would specify the action that the SBC should take if the pattern is found. In this case, the action would be to route the call to the specific location.

Using regex, the rule might look something like this:

“^212” -> Route to specific location

This rule would match any string (in this case, a caller’s phone number) that begins with the area code “212”, and would route the call to the specified location.

Example 2:

In this example, we want to create a rule in an SBC that routes calls from certain phone numbers to a voicemail system.

To do this, we would first define the pattern that we want the SBC to look for. In this case, the pattern would be the phone numbers that we want to match. Let’s say the phone numbers we want to match are “555-1212” and “555-1213”.

Next, we would specify the action that the SBC should take if the pattern is found. In this case, the action would be to route the call to the voicemail system.

Using regex, the rule might look something like this:

“555-1212|555-1213” -> Route to voicemail system

This rule would match any string (in this case, a caller’s phone number) that is either “555-1212” or “555-1213”, and would route the call to the voicemail system.

Example 3:

In this example, we want to create a rule in an SBC that routes calls from certain extensions to a specific location within the same organization.

To do this, we would first define the pattern that we want the SBC to look for. In this case, the pattern would be the extensions that we want to match. Let’s say the extensions we want to match are “1000” through “1999”.

Next, we would specify the action that the SBC should take if the pattern is found. In this case, the action would be to route the call to the specific location within the organization.

Using regex, the rule might look something like this:

“1[0-9]{3}” -> Route to specific location

This rule would match any string (in this case, a caller’s extension) that begins with the digit “1” and is followed by three more digits between “0” and “9”, and would route the call to the specified location within the organization.

Example 4:

In this example, we want to create a rule in an SBC that routes calls from international phone numbers to a specific location.

To do this, we would first define the pattern that we want the SBC to look for. In this case, the pattern would be the international phone numbers that we want to match. Let’s say the phone numbers we want to match are those that start with the “+” symbol, followed by any combination of digits.

Next, we would specify the action that the SBC should take if the pattern is found. In this case, the action would be to route the call to the specific location.

Using regex, the rule might look something like this:

“^+\d+” -> Route to specific location

This rule would match any string (in this case, a caller’s phone number) that starts with the “+” symbol, followed by one or more digits, and would route the call to the specified location.

Example 5:

In this example, we want to create a rule in an SBC that routes calls from certain phone numbers to a voicemail system, unless the call is coming from a specific area code.

To do this, we would first define the pattern that we want the SBC to look for. In this case, the pattern would be the phone numbers that we want to match, except for those from the specific area code. Let’s say the phone numbers we want to match are those that are not from the area code “212”, and the specific area code we want to exclude is “212”.

Next, we would specify the action that the SBC should take if the pattern is found. In this case, the action would be to route the call to the voicemail system.

Using regex, the rule might look something like this:

“^(?!212)\d+” -> Route to voicemail system

This rule would match any string (in this case, a caller’s phone number) that is not from the area code “212”, and would route the call to the voicemail system. The “?!212” portion of the pattern uses negative lookahead to exclude phone numbers from the area code “212”.

These are just a few examples of how regex rules might be created in SBCs to route calls based on certain patterns in the caller’s information. By understanding the basic concepts of regex and how to use them effectively, you can create powerful and flexible rules for routing calls in your SBC.

Summary

In this article, we covered the basics of regex and how it is commonly used in session border controllers. We also provided a brief tutorial on some of the most commonly used regex concepts, including anchors, wildcards, character classes, grouping and alternation, repetition, and special characters.

Regex is a powerful tool that is used in many fields to match, search, and manipulate strings of text. In the context of SBCs, regex is used to create rules for routing calls based on certain patterns in the caller’s information. By understanding the basics of regex and how to use it effectively, you can create powerful and flexible rules for routing calls in your SBC.