简体   繁体   English

从oracle sql中的字符串中提取特殊字符

[英]extracting special character from a string in oracle sql

I need a query to remove all alphanumeric characters from a string and give me only special characters.我需要一个查询来从字符串中删除所有字母数字字符并只给我特殊字符。

If string is '@#45gr@@3' query should give me '@#@@' .如果字符串是'@#45gr@@3'查询应该给我'@#@@'

SELECT REGEXP_REPLACE('@#45gr@@3','[^[:punct:]'' '']', NULL) FROM dual;

The old-fashioned way, with a replace() call:老式的方式,使用replace()调用:

select translate(upper(txt)
                 , '.1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                 , '.') as spec_txt
from t42
/

With a replace() solution is better to type all the characters we want to exclude.使用replace()解决方案更好地输入我们想要排除的所有字符。 Yes that is the larger set (probably) but at least it's well-defined.是的,这是更大的集合(可能),但至少它是明确定义的。 We don't want to keep revising the code to handle new characters in the input string.我们不想继续修改代码来处理输入字符串中的新字符。

Obviously regex solution is more compact and who can deny the elegance of @venkatesh solution ?显然正则表达式解决方案更紧凑,谁能否认@venkatesh 解决方案的优雅 However, evaluating regular expressions is more expensive than more focused SQL functions, so it's worth knowing alternative ways of doing things, for those cases when performance is an issue.然而,评估正则表达式比更集中的 SQL 函数更昂贵,因此对于那些性能成为问题的情况,了解其他做事方式是值得的。

Everything written in comments is most probably very true (especially the 5th one that talks about exceptions, special cases etc.).评论中写的所有内容很可能都是真实的(尤其是第 5 条关于异常、特殊情况等的内容)。 My feeling is that Jörg knows more about regular expressions than I'll ever know.我的感觉是 Jörg 对正则表达式的了解比我所知道的要多。

Anyway, to cut a long story short, in its very simple appearance, regarding the question posted ("remove all numbers and letters"), something like this might work:无论如何,长话短说,就其非常简单的外观而言,关于发布的问题(“删除所有数字和字母”),这样的事情可能会奏效:

SQL> select regexp_replace('a@#45gr@@3$.', '[[:digit:]]+|[[:alpha:]]+', '') result
  2  from dual;

RESULT
------
@#@@$.

SQL>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM