FreeSWITCH 1.8
上QQ阅读APP看书,第一时间看更新

Regular Expressions

Regular expressions (regexes) are almost omnipresent in FreeSWITCH configuration. They are "formulas" used to describe a string of text. For example, with a regular expression (regex) you can denotes all of: "cats", "cat", "supercat", "cAt" and "anycAts". That regex would be: ^.*c[aA]t.?$.. Explanation: caret ("^") means the beginning of the string. Then the point means "whatever character". The asterisk denotes an indefinite quantity (zero or more) of the preceding character - in this case of the "whatever character". The character "c" denotes... the character "c". Then the square brackets (and their content) denote a character that can be one of the list enumerated inside the square brackets (so, in this case the character can be "small a" or "capital a"). Then the character "t" is a placeholder for... the character "t". Then the point for "whatever character".

Then the question mark that establish the quantity of the preceding character in "zero or one instances", in our case means that after "t" can be one optional (eg, one or zero instances) of "whatever character". The dollar sign means the end of the string. Obviously you can have simpler regexes: "cat.*" would describes both "cat", "cats", "catharsis", etc.

Also, you can select one or more substrings from a string, by applying a regex to that string. Suppose you have the string "francesca bella ballerina" (eg: francesca, space, bella, space, ballerina), you can apply the regex "([a-z]*) ([a-z]*) ([a-z]*)" to that string. That regex (if successful) will split the string into three tokens, each one composed by small characters a to z. Eg, you have split using the space character as token delimiter, and you retained the sequences of a-z characters. Those sequences (that is, the tokens you split from the original string) are automatically put inside the $1, $2, $3 placeholders. So, you can use those placeholders and another regex to build a different string: "$1 $3 molto $2". That would results in the new string: "francesca ballerina molto bella", and she's actually a very beautiful dancer.

This micro intro gives you an idea of how powerful regexes can be for FreeSWITCH configuration. You can describe patterns of extensions, DIDs, users, callers, callees, gateways, etc. You can select, slice, dice and mix them. Logic can be built if that regex matches (eg, if it describes the string) or if that other regex is unsuccessful (it does not match the string). We'll see much more about all this.