0

在下面的示例中:

WITH X(DATA, ORD)
AS
(         
Select '@asdf@'   , 01 FROM DUAL UNION
Select '\qw@er\'  , 02 FROM DUAL UNION
Select '-zxcv-@' , 03  FROM DUAL UNION
Select '_poiu@' ,  04 FROM DUAL 
)

SELECT
REGEXP_REPLACE(DATA, '[@\-_]', '', 1)
FROM X
ORDER BY ORD;  
;  

我得到这个回应:

asdf 
qwer
-zxcv- 
poiu

但我想替换“@”、“\”、“-”或“_”,前提是它是字符串上的第一个字符,而不是中间。另外,它不适用于“-”字符。

4

2 回答 2

2

以下正则表达式应该可以工作:^[@\\_-]. 请注意,破折号-必须是字符类中的最后一个字符。

WITH X AS (
  SELECT '@asdf@' AS data, 01 AS ord FROM DUAL UNION
  SELECT '\qw@er\', 02 FROM DUAL UNION
  SELECT '-zxcv-@', 03  FROM DUAL UNION
  SELECT '_poiu@',  04 FROM DUAL 
)
SELECT REGEXP_REPLACE(DATA, '^[@\\_-]')
 FROM X
ORDER BY ORD
于 2015-02-10T22:56:11.950 回答
1

相反,使用这个

REGEXP_REPLACE(DATA, '^[@|\\|-|_]', '', 1)

注意^指定字符串开头的字符。

查询

WITH X(DATA, ORD)
AS
(         
Select '@asdf@'   , 01 FROM DUAL UNION
Select '\qw@er\'  , 02 FROM DUAL UNION
Select '-zxcv-@' , 03  FROM DUAL UNION
Select '_poiu@' ,  04 FROM DUAL 
)
SELECT
REGEXP_REPLACE(DATA, '^[@|\\|-|_]', '', 1)
FROM X
ORDER BY ORD; 

给了我结果

asdf@, qw@er, zxcv@, poiu@,

于 2015-02-10T21:23:16.947 回答