DB2 SQL 中的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4763757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expressions in DB2 SQL
提问by Samer Buna
(Other than using a UDF) Any REGEXP-In-SQL support for DB2 9.7 ?
(除了使用 UDF)对 DB2 9.7 的任何 REGEXP-In-SQL 支持?
采纳答案by tessus
There is no built-in support for regular expressions in DB2 9.7.
DB2 9.7 中没有对正则表达式的内置支持。
The only way is using UDFs or table functions as described in the article 'OMG Ponies' added in the comment.
唯一的方法是使用 UDF 或表函数,如评论中添加的文章“OMG Ponies”中所述。
@dan1111: I do not appreciate my post being edited, especially if people can't read the question correctly. The OP asked Any REGEXP-In-SQLsupport for DB2 9.7
@dan1111:我不喜欢我的帖子被编辑,特别是如果人们无法正确阅读问题。OP 询问对 DB2 9.7 的任何 REGEXP-In- SQL支持
SQL is not XQuery !!!
SQL 不是 XQuery !!!
Sorry, don't delete the text of my 100% correct answer. You can add a comment or write your own answer.
抱歉,不要删除我 100% 正确答案的文字。您可以添加评论或编写自己的答案。
回答by komikoni
I'm komikoni(Keisuke Konishi).
我是komikoni(小西圭介)。
I created the regular expression function (UDF) which does not exist in db2. The UDF using the SQL/XML(Xquery). You can easily install.
我创建了 db2 中不存在的正则表达式函数 (UDF)。使用 SQL/XML(Xquery) 的 UDF。您可以轻松安装。
List of regular expressions provide UDF
提供UDF的正则表达式列表
- REG_MATCHESprovides Coincidence existence ( Scalar )
- REG_REPLACEstring substitution ( Scalar )
- REG_COUNTnumber of matches retrieved ( Scalar )
- REG_POSITIONmatch position acquisition ( Scalar )
- REG_SUBSTRgets a string matching ( Scalar )
- REG_SUBSTR_TABLElist of matching string information ( Table )
- REG_TOKENIZE_TABLElist of mismatched string information (divided by a separator string) ( Table )
- REG_ALLTOKEN_TABLElist of mismatch string and matching string information ( Table )
- REG_MATCHES提供巧合存在(标量)
- REG_REPLACE字符串替换(标量)
- REG_COUNT检索到的匹配项数(标量)
- REG_POSITION匹配位置获取(标量)
- REG_SUBSTR获取字符串匹配(标量)
- REG_SUBSTR_TABLE匹配字符串信息列表(表)
- REG_TOKENIZE_TABLE不匹配字符串信息列表(由分隔符字符串分隔)(表)
- REG_ALLTOKEN_TABLE不匹配字符串和匹配字符串信息列表(表)
Scripts can be downloaded from here. (Sorry in Japanese)
脚本可以从这里下载。(日语抱歉)
https://www.ibm.com/developerworks/jp/data/library/db2/j_d-regularexpression/
https://www.ibm.com/developerworks/jp/data/library/db2/j_d-regularexpression/
(English : Machine translationScript : The last of a Japanese page)
(英语:机器翻译脚本:日语页面的最后)
I look forward to your feedback and comments.
我期待着您的反馈和意见。
回答by AngocA
The real answer is that DB2 doessupport regular expression since PureXML was added (v9.7 included) via xQuery with the matches function.
真正的答案是 DB2确实支持正则表达式,因为 PureXML 是通过具有匹配功能的 xQuery 添加的(包括 v9.7)。
For example:
例如:
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
)
select * from val"
For more information:
想要查询更多的信息:
回答by AidanH
That works fine except for DB2 z/OS - in DB2 v10 z/OS you must use PASSING as follows
除了 DB2 z/OS 之外,它工作正常 - 在 DB2 v10 z/OS 中,您必须按如下方式使用 PASSING
with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($v,"^[A-Za-z 0-9]*$")'
PASSING t.text as "v" ) as integer) = 0
)
select * from val
回答by data_henrik
Starting with DB2 11.1 there is built-in regex support. One of the new function is REGEXP_SUBSTR
and there are some more.
从 DB2 11.1 开始,有内置的正则表达式支持。其中一项新功能是REGEXP_SUBSTR
,还有更多功能。
SELECT REGEXP_SUBSTR('hello to you', '.o',1,1)
FROM sysibm.sysdummy1