How can I replace brackets to hyphens within Oracle REGEXP_REPLACE function?

后端 未结 1 1679
离开以前
离开以前 2021-01-25 06:58

I\'m a newbie in regular expressions. I want to replace any text string symbols like (,),[,] to hyphens (for example):

SELECT REGEXP_REPLACE (\'()\',          


        
相关标签:
1条回答
  • 2021-01-25 07:39

    To replace symbols, use the TRANSLATE function, it is less processor-intensive than regular expression functions:

    SQL> SELECT translate ('(<FIO>)', '()[]', '----') replaced FROM dual;
    
    REPLACED
    --------
    -<FIO>-
    

    Regular expressions are more versatile and can do more complex things but are more expensive. In this case, replacing one character by another is done more efficiently by a specialized function. If you really want to use regular expressions, you could use REGEXP_REPLACE:

    SQL> SELECT regexp_replace ('[(<FIO>)]', '[]()[]', '-', 1, 0) reg FROM dual;
    
    REG
    ---------
    --<FIO>--
    

    Update: If you want to replace only the first symbol, translate won't work. Instead, use:

    SQL> SELECT regexp_replace ('[(<FIO>)]', '[]()[]', '-', 1, 1) reg FROM dual;
    
    REG
    ---------
    -(<FIO>)]
    
    0 讨论(0)
提交回复
热议问题