Difference between revisions of "M4 Regexp Examples"

From Kolmisoft Wiki
Jump to navigationJump to search
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Basic Regexp operators and building blocks are explained [https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended here].
Basic Regexp operators and building blocks are explained [https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended here].


<br><br>
= Regexp for matching =
= Regexp for matching =


Line 11: Line 12:
** CLD/DNIS Deny (Blacklist)
** CLD/DNIS Deny (Blacklist)
* [[M4 Termination Points | Termination Points]]
* [[M4 Termination Points | Termination Points]]
** Source (CallerID) Allow
** CLI/ANI Allow (Whitelist)
** Source (CallerID) Deny
** CLI/ANI Deny (Blacklist)
** CLD/DNIS Allow (Whitelist)
** CLD/DNIS Deny (Blacklist)
* [[M4 Dial Peers | Dial Peers]]
* [[M4 Dial Peers | Dial Peers]]
** Source regexp
** Source regexp
Line 18: Line 21:
** Destination Regexp
** Destination Regexp
** Destination deny regexp
** Destination deny regexp


==== Validate regex  ====
==== Validate regex  ====
Line 51: Line 55:
|-
|-
|}
|}
<br><br>


= Regexp for transformation =
= Regexp for transformation =
Line 57: Line 63:


* [[M4 Origination Points | Origination Points]]
* [[M4 Origination Points | Origination Points]]
** Regexp Transformation Rule
** CLI/ANI Regexp Transformation Rule
** PAI Transformation
** CLD/DNIS Regexp Transformation Rule
** RPID Transformation
** [https://wiki.kolmisoft.com/index.php/M4_Kamailio_Transformations PAI Transformation]
** [https://wiki.kolmisoft.com/index.php/M4_Kamailio_Transformations RPID Transformation]
* [[M4 Termination Points | Termination Points]]
* [[M4 Termination Points | Termination Points]]
** RPID from OP Transformation
** CLI/ANI Regexp Transformation Rule
** PAI from OP Transformation
** CLD/DNIS Regexp Transformation Rule
** [https://wiki.kolmisoft.com/index.php/M4_Kamailio_Transformations RPID from OP Transformation]
** [https://wiki.kolmisoft.com/index.php/M4_Kamailio_Transformations PAI from OP Transformation]


==== Format ====
==== Format ====
Line 71: Line 80:


* match_expression - POSIX regular expression
* match_expression - POSIX regular expression
* replacement_expression - replacement expression with back references to matched tokes: \1, \2, …, \9
* replacement_expression - replacement expression with back references (explained [https://wiki.kolmisoft.com/index.php/M4_Kamailio_Transformations#Replacement_Expression here]) to matched tokes: \1, \2, …, \9
* flags:
* flags (optional):
** i - match ignore case
** i - match ignore case
** g - replace all matches
** g - replace all matches


Before putting Transformations into production, it is advisable to test them on various values.  
Before putting Transformations into production, it is advisable to test them on various values.


==== Test in Linux console ====
==== Test in Linux console ====
Line 84: Line 93:
  echo '00370123456789' | sed -E 's/^00//'
  echo '00370123456789' | sed -E 's/^00//'


==== Test in online sed live editor r====
==== Test in online sed live editor ====


If you do not have access to the Linux console, use https://sed.js.org/ (or any other online sed editor).
If you do not have access to the Linux console, use https://sed.js.org/ (or any other online sed editor).
Line 103: Line 112:
| /\+?(.*)/+\1/ || 370123456789 || +370123456789 || Add + if it is not present already
| /\+?(.*)/+\1/ || 370123456789 || +370123456789 || Add + if it is not present already
|-
|-
| ^/.*/999\1/ || 370123456789 || 999370123456789 || Add 999 in front of any string
| /(.*)/999\1/ || 370123456789 || 999370123456789 || Add 999 in front of any number
|-
| /(.*)/999$CIP\1/ || 370123456789 || 999888370123456789 || $CIP is a variable that contains Code from MNP database (if MNP is used and if number is matched). This variable only works on Destination Transformation Regexp. The example assumes that there is a record in the MNP database with Code 888 for number 370123456789.
|-
|-
|}
|}
Line 110: Line 121:


= See also =
= See also =
* [[M4 Kamailio Transformations]]
* [[M4 Source Transformation]]
* [[M4 Source Transformation]]
* [[M4 Destination Transformation]]
* [[M4 Destination Transformation]]
* Standard: http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended
* Standard: http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended
* Check your regexp: http://regex101.com/
* Check your regexp: http://regex101.com/

Latest revision as of 07:35, 26 February 2024

Basic Regexp operators and building blocks are explained here.



Regexp for matching

Regexp matching is used in:

  • Origination Points
    • CLI/ANI Allow (Whitelist)
    • CLI/ANI Deny (Blacklist)
    • CLD/DNIS Allow (Whitelist)
    • CLD/DNIS Deny (Blacklist)
  • Termination Points
    • CLI/ANI Allow (Whitelist)
    • CLI/ANI Deny (Blacklist)
    • CLD/DNIS Allow (Whitelist)
    • CLD/DNIS Deny (Blacklist)
  • Dial Peers
    • Source regexp
    • Source deny regexp
    • Destination Regexp
    • Destination deny regexp


Validate regex

The regex can be validated using https://regexr.com/ (or any other online regex editor).

Examples

Regexp Explanation
.* everything
^[0-9]+$ only digits
^[0-9]{9,}$ only digits, number more than 8 digits (9, 10, 11...)
^[0-9]{0,9}$ only digits, number less than 9 digits (1, 2, 3, 4, 5, 6, 7, 8)
^\\+?[0-9]+$ optional + sign and only digits
^370[0-9]+$ only digits starting with 370
^370.*$ anything starting with 370
^(37[0-1]|44)[0-9]*$ only digits starting with 370 or 371 or 44
^380(67|97|68|98)[0-9]*$|^998[0-9]+$ only digits starting with 380(67,97,68,98) or 998(0-9), where 380(67,97,68,98) = 38067,38097,38068,38098 and 998(0-9) = 9980,...,9988,9989.
^380(67|97|68|98)[0-9]{5,9}$|^998[0-9]{5,9}$ only digits starting with 380(67,97,68,98) or 998, but number of digits after each prefix should be between 5 and 9. So, for example, 3809712345 will match, but 380971234 or 380971234567890 will NOT match.
^370[138][0-9]+$ only digits starting with 3701, 3703, 3708



Regexp for transformation

Regexp transformation is used in:

Format

Regexp transformation format is:

/match_expression/replacement_expression/flags

  • match_expression - POSIX regular expression
  • replacement_expression - replacement expression with back references (explained here) to matched tokes: \1, \2, …, \9
  • flags (optional):
    • i - match ignore case
    • g - replace all matches

Before putting Transformations into production, it is advisable to test them on various values.

Test in Linux console

Execute sed command (add s before transformation s/regex/replacement/flags):

echo '00370123456789' | sed -E 's/^00//'

Test in online sed live editor

If you do not have access to the Linux console, use https://sed.js.org/ (or any other online sed editor).

Examples

Transformation Original number Modified number Comment
/^00// 003701234567 003701234567 Cut prefix 00
/^370/8/ 370123456789 8123456789 Replace 370 with 8
/\+// +370123456789 370123456789 Cut + (here + is escaped by \ since it is special regex symbol)
/\+370/86/ +370123456789 86123456789 Cut prefix +370, add prefix 86
/\+?(.*)/+\1/ 370123456789 +370123456789 Add + if it is not present already
/(.*)/999\1/ 370123456789 999370123456789 Add 999 in front of any number
/(.*)/999$CIP\1/ 370123456789 999888370123456789 $CIP is a variable that contains Code from MNP database (if MNP is used and if number is matched). This variable only works on Destination Transformation Regexp. The example assumes that there is a record in the MNP database with Code 888 for number 370123456789.



See also