如何在Oracle REGEXP_REPLACE函数中将括号替换为连字符? [英] How can I replace brackets to hyphens within Oracle REGEXP_REPLACE function?
问题描述
我是正则表达式的新手.我想将(,),[,]之类的任何文本字符串符号替换为连字符(例如):
I'm a newbie in regular expressions. I want to replace any text string symbols like (,),[,] to hyphens (for example):
SELECT REGEXP_REPLACE ('(<FIO>)', '(', '-') FROM dual;
这给我ORA-12725错误.
This gives me ORA-12725 error.
请解释一下,怎么了? 谢谢.
Please, explain me, what's wrong? Thanks.
推荐答案
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>-
正则表达式具有更多用途,可以执行更复杂的操作,但价格更高.在这种情况下,通过专用功能可以更有效地完成一个字符的替换.如果您确实要使用正则表达式,则可以使用 REGEXP_REPLACE
:
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>)]
这篇关于如何在Oracle REGEXP_REPLACE函数中将括号替换为连字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!