开发者

SQL Regular Expressions

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

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 开发者_JAVA技巧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?


You can do this using LIKE.

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

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


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

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


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.

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}\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 [ -]

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜