SQL 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4911871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:13:11  来源:igfitidea点击:

SQL Regular Expressions

sqlsql-serverregex

提问by Sudantha

I created the following SQL regex pattern for matching an ISBN:

我创建了以下 SQL 正则表达式模式来匹配 ISBN:

CREATE RULE ISBN_Rule AS @value LIKE 'ISBN\x20(?=.{13}$)\d{1,5}([-])\d{1,7}\d{1,6}(\d|X)$'

I used the following values as test data; however, the data is not being committed:

我使用以下值作为测试数据;但是,数据没有被提交:

ISBN 0 93028 923 4 | ISBN 1-56389-668-0 | ISBN 1-56389-016-X

Where am I wrong?

我哪里错了?

采纳答案by gbn

You can do this using LIKE.

您可以使用 LIKE 执行此操作。

You'll need some ORs to deal with the different ISBN 10 and 13 formats

您需要一些 OR 来处理不同的 ISBN 10 和 13 格式

For the above strings:

对于上述字符串:

LIKE 'ISBN [0-9][ -][0-9][0-9][0-9][0-9][0-9][ -][0-9][0-9][0-9][ -][0-9X]'

回答by kelloti

The LIKEoperator in SQL Server isn't a regex operator. You can do some complicated pattern matching, but its not normal regex syntax.

LIKESQL Server 中的运算符不是正则表达式运算符。你可以做一些复杂的模式匹配,但它不是正常的正则表达式语法。

http://msdn.microsoft.com/en-us/library/ms179859.aspx

http://msdn.microsoft.com/en-us/library/ms179859.aspx

回答by RichardTheKiwi

SQL Server 2005 does not support REGEX expressions out of the box, you would need OLE Automation or a CLR to provide that functionality through a UDF.

SQL Server 2005 不支持现成的 REGEX 表达式,您需要 OLE 自动化或 CLR 来通过 UDF 提供该功能。

The only supported wildcards are % (any) and _ (one), and character range (or negation) matches using [] optionally [^]. So your expression

唯一支持的通配符是 %(任意)和 _(一),以及使用 [] 可选的 [^] 匹配字符范围(或否定)。所以你的表情

'ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\d{1,6}(\d|X)$'

Means something very weird with the range [- ] and everything else being literal.

表示范围 [- ] 和其他所有内容都是文字非常奇怪的东西。

回答by RichardTheKiwi

If it splits on | and doesen't strip whitespaces, its probably missing a space before ISBN and/or after (\d|X) here$ .. Also, I doubt this is the problem, but [- ] could be [ -]

如果它分裂于 | 和doesen't带空格,它可能缺少ISBN前的空间和/或之后|(\ d X)这里$ ..而且,我怀疑这是问题,但[ - ]可能是[ - ]

edit: ok, well keep this in mind when you get a regex lib/control.

编辑:好的,当您获得正则表达式库/控件时,请记住这一点。