简体   繁体   中英

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 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. You can do some complicated pattern matching, but its not normal regex syntax.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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