Regular Expressions For SIP Trunk



The regular expression is a sequence of characters that form a search pattern. See the definition in Wikipedia. It is a huge topic and takes a lot of time to explain. If you didn’t use it, you will get stuck in a nested “If” statements situation. Using regular expressions means professionalism. I am assuming you know about it and i will just apply it to SIP.


Regular Expressions For a SIP Trunk

Usually when a SIP router receives a SIP request which is addressed to a PSTN gateway (i.e. Its Request-URI contains a telephone number), it checks which group the requested user (called party) belongs to. The router also must check if the caller has permission to make this call. So the check can be done on the caller and calle. Any check done by the router is translated into this simple thinking logic:

If (Routing Condition){

  Do Specific Work


This can be in the router’s routing script or internally integrated in the router’s application.


If (The Call Is Addressed To A Local Number){

Do Specific Work


In OpenSIPS SIP router, the previous condition can be treated in the routing scripts in two ways:

  • Using the Script Variables (e.g. the reference to the request’s uri ($ru)) with the regular expression directly:

If ($ru=~sip:[2-9][0-9]{6}@){


The previous regular expression is for any 7 numbers starting by one digit from the range [2-9].

  • Using a function like “pcre_match” in “Regex” module which matches the given string against a given regular expression. The return value is TRUE if it is matched, FALSE otherwise:

If (pcre_match(“$ru”,sip:[2-9][0-9]{6}@){


The variable “$ru” is read/write variable so be aware where in the script you are checking its value. You can use “$ou” which is a reference to the request’s original URI. The module “Regex” is based on the library “PCRE” which is an implementation of regular expression pattern matching where the regular expression parameter will be compiled in PCRE object. So the development libraries of “PCRE” must be installed (“libpcre-dev” or “pcre-devel”).

Note: Use “&&” if you want to concatenate multiple conditions in one “if” statement.

If ((Routing Condition-1) && (Routing Condition-2)){

  Do Specific Work


Examples of Regular Expressions For SIP Trunk

You should test your regular expression before using it to know if you have constructed it correctly. There are many online free services that you can use to do your tests. Use search engines to search for syntax symbols that you can use to construct the regular expression. The following are examples. Note that each one defines a group:

  • Any user on any domain/IP: sip:(.*)@(.*)
  • Any user on certain domain: sip:(.*)
  • Any user on certain IP (  sip:(.*)@198\.18\.250\.10 In regular expression, the “.” is interpreted as “any character” symbol whereas “\.” is just a period (dot).
  • Any user on IP Range ( – sip:(.*)@198\.18\.250.* The symbol “*” is quantifier which means the preceding character is found 0 or more times. Here i left the last part of IP to be anything but you can restrict it.
  • To group a set of SIP URIs that are within a certain domain or certain subdomain, use .*mydomain\.com.* For example these URIs will be matched:;transport=tcp, and SIP/2.0
  • To group a set of SIP URIs that have certain string in the username part of URI and the ports 5060 and 5061 are accepted, use the regular expression .*group1@198\.18\.250.\10:506<01>.* For example these two URIs will be matched sip:serv1group1@;transport=tcp and sips:serv2group1@;tranport=tcp
  • 8-digit number on any domain: sip:[0-9]{8}@(.*) The [0-9] is a range for one number between 0 and 9 and {8} means repeat the preceding number 8 times.
  • 8-digit number starting optionally by 8 on any domain: sip:8?[0-9]{7}@(.*)  The symbol “?” means the repetition is 0 or 1 to the preceding. For example,,, and so on.
  • 4-digit number (could be an extension number) starting by 6 on a certain domain: sip:6[0-9]{3}@(.*)
  • 4-digit number which is not starting by 55 on any domain: sip:(?!55)[0-9]{4}@(.*)

 More Information


SIP & SIPS Uniform Resource Indictor (URI)



SIP or SIPS URI is a logical address. Generally it is not tight to a single physical device. SIP user can register itself and make calls from any device connected to the internet and has SIP client installed and configured. SIP/SIPS URI identifies a communication resource (e.g. user of an online service, a group such as “sales” or “helpdesk” in an organization , mailbox on a messaging system, PSTN number at a gateway service). SIPS is used when SIP messages are protected by TLS across each hop (the communication resource is contacted securely).

Due to the mobility of SIP/SIPS URI, the user needs to periodically register its physical location with its SIP proxy/registrar (sending SIP REGISTER Request periodically). So the SIP/SIPS URI is mapped to its physical location (FQDN/IP Address) after registration. The physical location is kept in Contact header of the SIP REGISTER Request. The mapping between SIP/SIPS URI and the physical location is stored in the database of the SIP router and used when the router wants to forward a call to a user or group.

SIP URIs are used in “To”, “From”, “Contact” SIP Message’s headers, as well as in Request-URI. The general form of SIP URI is sip:user:password@host:port;uri-parameters?headers. The same format is for SIPS but the scheme is SIPS instead of SIP. The userinfo “user:password”  is optional (for example it is not exist in the URI of the proxy. e.g. If the @ sign is present , the user field MUST NOT be empty. The user field can be a telephone number. The password field is not recommended because of security reason (password in clear text). The host portion “host:port” represent where the request has to be sent (e.g. proxy/registrar). The host can be FQDN (recommended) or IP address.

SIP URI Parameters

 URI parameters comes after the portion “host:port” and are separated by semicolon “;“. The parameters are: “cause” RFC 4458, “transport” RFC 3261, “comp”, RFC 3486, “content-type”, RFC 4240, “delay”, RFC 4240, “duration” ,RFC 4240, “local”, RFC 4240, “param[n]”, RFC 4240, “play”, RFC 4240, “repeat”, RFC 4240, “gr” RFC 5627, “lr” RFC 3261, “maddr”, RFC 3261, “method”, RFC 3261, “ob” RFC 5626, “sigcomp-id”, RFC 5049, “target”, RFC 4240,4458, “ttl”, RFC 3261, “user”, RFC 3261, “voicexml”, RFC 4240.

Examples;transport=tcp  #transport is a parameter;maddr=;ttl=15

SIP URI Headers

SIP URI Headers comes after “?“. The format is Header Name = Header Value.

SIP & SIPS URI Examples taken from RFC 3261;transport=tcp;user=phone

Comparison between SIP/SIPS URI is case sensitive for the portion “user:password”. Characters other than reserved, can be written in HEX. Example: These URIs are equivalent:;transport=TCP

 Converting tel URL to SIP URI

The tel URL is specified in RFC 2806. The portion telephone-subscriber of the tel URL is placed into userinfo part of the SIP/SIPS URI. The telephone-subscriber can be local or global phone number.

Thus, tel:+358-555-1234567;postd=pp22



 More Information