Is there any regular expression library written in T-SQL (no CLR, no extended sp, pure t-sql) for SQL Server?
(should work with shared hosting)
Edit:
thanks I know about PATINDEX, LIKE, xp_ sps and CLR solutions
I also know it is not the best place for regex, the question is theoretical:)
reduced functionality is also accepted
If anybody is interested in using regex with CLR here is a solution. The function below (C# .net 4.5) returns a 1 if the pattern is matched and a 0 if the pattern is not matched. I use it to tag lines in sub queries. The SQLfunction attribute tells sql server that this method is the actual UDF that SQL server will use. Save the file as a dll in a place where you can access it from management studio.
In management studio import the dll file via programability -- assemblies -- new assembly
Then run this query:
Then you should have complete access to the function via the database you stored the assembly in.
Then use in queries like so:
There is some basic pattern matching available through using LIKE, where % matches any number and combination of characters, _ matches any one character, and [abc] could match a, b, or c... There is more info on the MSDN site.
In case anyone else is still looking at this question, http://www.sqlsharp.com/ is a free, easy way to add regular expression CLR functions into your database.
If you are using SQL Server 2016 or above, you can use
sp_execute_external_script
along with R. It has functions for Regular Expression searches, such asgrep
andgrepl
.Here's an example for email addresses. I'll query some "people" via the SQL Server database engine, pass the data for those people to R, let R decide which people have invalid email addresses, and have R pass back that subset of people to SQL Server. The "people" are from the
[Application].[People]
table in the[WideWorldImporters]
sample database. They get passed to the R engine as a dataframe namedInputDataSet
. R uses the grepl function with the "not" operator (exclamation point!) to find which people have email addresses that don't match the RegEx string search pattern.Note that the appropriate features must be installed on the SQL Server host. For SQL Server 2016, it is called "SQL Server R Services". For SQL Server 2017, it was renamed to "SQL Server Machine Learning Services".
Closing Thoughts Microsoft's implementation of SQL (T-SQL) doesn't have native support for RegEx. This proposed solution may not be any more desirable to the OP than the use of a CLR stored procedure. But it does offer an additional way to approach the problem.
How about the PATINDEX function?
The pattern matching in TSQL is not a complete regex library, but it gives you the basics.
(From Books Online)
You can use VBScript regular expression features using OLE Automation. This is way better than the overhead of creating and maintaining an assembly. Please make sure you go through the comments section to get a better modified version of the main one.
http://blogs.msdn.com/b/khen1234/archive/2005/05/11/416392.aspx
If you get
SQL Server blocked access to procedure 'sys.sp_OACreate'...
error, usesp_reconfigure
to enableOle Automation Procedures
. (Yes, unfortunately that is a server level change!)More information about the
Test
method is available hereHappy coding