English 中文(简体)
Replace characters in Asterisk Dialplan
原标题:

I want to change a couple off characters * # for A and P to have the monitor filename with characters more friendly. The only solution I could find was to do it my self within the dialplan but it generates a lot of verbosity output and is not as efficient(fast) as I would like to. I ll post it here just in case someone wants to use it. But I m looking for an asterisk function that I can compile something that I can call withing the dialplan like ${REPLACE(${EXTEN},*,a)} and have the exten **123**456*** converted to AA123AA456AAA.

;
; MACRO REPLACE
;
[macro-replace]
;
;   ${ARG1} - String source
;   ${ARG2} - Chars to replace
;   ${ARG3} - Chars to replace with
;
exten => s,1,NoOp(Replacing ${ARG2} for ${ARG3} in ${ARG1})
exten => s,n,Set(str=${ARG1})
exten => s,n,Set(find=${ARG2})
exten => s,n,Set(replace=${ARG3})
exten => s,n,Set(i=0)
exten => s,n,Set(length=${LEN(${str})})
exten => s,n,While($[${i} < ${length}])
exten => s,n,GotoIf($["${str:${i}:1}" != "${find}"]?continue)
exten => s,n,Set(pre=)
exten => s,n,GotoIf($["${i}" = "0"]?post)
exten => s,n,Set(pre=${str:0:${i}})
exten => s,n(post),Set(post=)
exten => s,n,GotoIf($["${i}" = $[${length} - 1]]?write)
exten => s,n,Set(post=${str:$[${i} + 1]})
exten => s,n(write),Set(str=${pre}${replace}${post})
exten => s,n(continue),Set(i=$[${i} + 1])
exten => s,n,EndWhile
exten => s,n,Set(REPLACERESULT=${str})

问题回答

The REPLACE() function now does this easily:

exten => 100,1,Set(find=**123**456***)
same => n,NoOp(find=${find})
same => n,Set(replace=${REPLACE(find,*,A)})
same => n,NoOp(find=${find}, replace=${replace})
same => n,hangup()

Output:

*CLI> channel originate local/100@default extension null@default
    -- Executing [100@default:1] Set("Local/100@default-c758;2", "find=**123**456***") in new stack
    -- Executing [100@default:2] NoOp("Local/100@default-c758;2", "find=**123**456***") in new stack
    -- Executing [100@default:3] Set("Local/100@default-c758;2", "replace=AA123AA456AAA") in new stack
    -- Executing [100@default:4] NoOp("Local/100@default-c758;2", "find=**123**456***, replace=AA123AA456AAA") in new stack
    -- Executing [100@default:5] Hangup("Local/100@default-c758;2", "") in new stack
  == Spawn extension (default, 100, 5) exited non-zero on  Local/100@default-c758;2  

That s really the best way to do it (without using regex). If you want to use regex (regular expressions), Asterisk 1.1+ has full support for it. This will allow you to do your entire macro in a single line. The documentation for using regex in dialplan is here: voip-info.

Hopefully this helps! There are plenty of examples on that voip-info page that should be able to help you along!

Another alternative to what you ve done is to use an AGI script. Just write your code in bash/python/etc and use it as AGI(replace,${arg1},${arg2},${arg3}). Might not be as fast as an internal function but it s more compact and potentially faster than your solution.





相关问题
complex batch replacement linux

I m trying to do some batch replacement for/with a fairly complex pattern So far I find the pattern as: find ( -name *.php -o -name *.html ) -exec grep -i -n hello {} + The string I want ...

HTML anchor replace with RegEx

I have HTML data which I ll be using in a client app. I need to Regex.Replace the <a> tags from <a href="Bahai.aspx">Bahai</a> to <a href="#" onclick="process( Bahai.aspx );...

Replace placeholders with text - C?

I use the below function to retrieve characters one by one from a FILE stream. Now the text in that file will sometimes include places holders like !first_name! within the text. What s the best way to ...

Extract float from text line with sed?

I am issuing a sed replace on a line, trying to extract a particular floating-point value, but all that appears to be matched is the right-side of the decimal Text Line: 63.544: [GC 63.544: [DefNew: ...

热门标签