简体   繁体   English

SQL正则表达式

[英]SQL Regular Expressions

I created the following SQL regex pattern for matching an ISBN: 我创建了以下用于匹配ISBN的SQL正则表达式模式:

CREATE RULE ISBN_Rule AS @value LIKE 'ISBN\x20(?=.{13}$)\d{1,5}([-])\d{1,7}\1\d{1,6}\1(\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? 我哪里错了?

The LIKE operator in SQL Server isn't a regex operator. SQL Server中的LIKE运算符不是正则表达式运算符。 You can do some complicated pattern matching, but its not normal regex syntax. 你可以做一些复杂的模式匹配,但它不是正常的正则表达式语法。

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

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]'

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 [^]. 唯一支持的通配符是%(any)和_(一),字符范围(或否定)匹配使用[]可选[^]。 So your expression 所以你的表达

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

Means something very weird with the range [- ] and everything else being literal. 意味着一些非常奇怪的范围[ - ]和其他一切都是字面的。

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. 编辑:好的,当你得到一个正则表达式lib /控件时,请记住这一点。

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

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