by Lutz » Sat Aug 16, 2003 1:31 pm
Before all, don't forget that 'replace' on strings only works in regular expression mode if you add the last options parameter, i.e.
(set 'str "abydxfg")
(replace "x|y" str "e") ;; will not work
(replace "x|y" str "e" 0) ;; will work nicely
(replace "x|y" str "e" 1) ;; will work case insensitive
Replace with regex subexpression is not yet implemented and I hope I will come around to do it one day. Unfortunately 'replace mode seems not to be part of the PCRE package I am using to bild newLISP regular expressions.
But you could use 'regex' to hack something together, because 'regex' returns parenthesized subexpressions:
(set "abcdxfg")
(regex "(..)x" str) -> ("cdx" 2 3 "cd" 2 2)
Now you could use 'slice', 'nth' and concat to compose you new string. But I am not sure that in the case of URL hex translation it will be smaller/faster than your solution, which looks quite elegant (lisp'yish) to me.
The is also a function for hex-code translation in cgi.lsp of the distribution, but your solution seems to be a lot shorter already.
Lutz