1

I want a regular Expression which replaces a string with its respective character in descending order including number: i.e.

  • replace a alphanumeric string with descending order string
  • if special characters are found in sting keep the as it is

I means :

A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z
Z   Y   X   W   V   U   T   S   R   Q   P   O   N   M   L   K   J   I   H   G   F   E   D   C   B   A

0   1   2   3   4   5   6   7   8   9
9   8   7   6   5   4   3   2   1   0

Few examples:

1) ABC*1230 -> ZYX*8739

2) Hello World! -> Svool Dliow!

3) Good & Bad -> Tllw & Yzw

4

2 回答 2

13

我会TRANSLATE为此使用 Oracle 函数:

TRANSLATE(myValue,
  'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
  'ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210')
于 2013-06-13T14:16:44.853 回答
3

这里是用 REGEXP 完成的:

SELECT  LISTAGG(DECODE(o_str_level,NULL,srch_l.rs,o_str_level)) WITHIN GROUP (ORDER BY srch_l.ri) AS the_result
FROM
(
        SELECT  REGEXP_SUBSTR(t_str, '.', 1, LEVEL) rs
        ,       REGEXP_INSTR (t_str, '.', 1, LEVEL) ri
        FROM    
        (
                SELECT  'ABCDEF TUVWXYZ' AS t_str FROM DUAL
        ) test_str
        CONNECT BY LEVEL <= LENGTH(t_str)
) srch_l
LEFT JOIN
(
        SELECT  REGEXP_SUBSTR(r_str, '\w', 1, LEVEL) AS r_str_level
        ,       REGEXP_SUBSTR(o_str, '\w', 1, LEVEL) AS o_str_level
        FROM
        (
                SELECT  'ABCDEFGHIJKLMNOPQRSTUVWXYZ' r_str, 'ZYXWVUTSRQPONMLKJIHGFEDCBA' o_str FROM DUAL
        )
        CONNECT BY LEVEL <= 26
) apt
ON      srch_l.rs = apt.r_str_level;

-- Result:
-- ZYXWVU GFEDCBA

它比翻译简单吗?几乎...

于 2013-06-13T15:28:13.800 回答