Click here to Skip to main content
14,982,989 members
Articles / Database Development / SQL Server
Posted 9 Aug 2003


76 bookmarked

xp_pcre - Regular Expressions in T-SQL

Rate me:
Please Sign up or sign in to vote.
4.87/5 (26 votes)
16 Mar 20059 min read
An Extended Stored Procedure to use regular expressions in T-SQL.


xp_pcre is a follow-up to my extended stored procedure xp_regex. Both allow you to use regular expressions in T-SQL on Microsoft SQL Server 2000. This version was written because xp_regex uses the .NET Framework, which many people were reluctant to install on their SQL Servers. (It turns out they were the smart ones: although installing the .NET Framework on a SQL Server is no cause for concern, I've been informed by several people that hosting the CLR inside the SQL Server process is a Bad Idea™. Please see the warnings on the xp_regex page for more information.)

xp_pcre is so named because it uses the "Perl Compatible Regular Expressions" library. This library is available at (You don't need to download the PCRE library in order to use xp_pcre. The library is statically linked.)


There are six extended stored procedures in the DLL:

  • xp_pcre_match
  • xp_pcre_match_count
  • xp_pcre_replace
  • xp_pcre_format
  • xp_pcre_split
  • xp_pcre_show_cache

The parameters of all of these procedures can be CHAR, VARCHAR or TEXT of any SQL Server-supported length. The only exception is the @column_number parameter of xp_pcre_split, which is an INT.

If any required parameters are NULL, no matching will be performed and the output parameter will be set to NULL. (Note: This is different than the previous version which left the parameters unchanged.)

1. xp_pcre_match


EXEC master.dbo.xp_pcre_match @input, @regex, @result OUTPUT
  • @input is the text to check.
  • @regex is the regular expression.
  • @result is an output parameter that will hold either '0', '1' or NULL.

xp_pcre_match checks to see if the input matches the regular expression. If so, @result will be set to '1'. If not, @result is set to '0'. If either @input or @regex is NULL, or an exception occurs, @result will be set to NULL.

For example, this will determine whether the input string contains at least two consecutive digits:

EXEC master.dbo.xp_pcre_match 'abc123xyz', '\d{2,}', @out OUTPUT
PRINT @out

prints out:


This one will determine whether the input string is entirely comprised of at least two consecutive digits:

EXEC master.dbo.xp_pcre_match 'abc123xyz', '^\d{2,}$', @out OUTPUT
PRINT @out

prints out:


2. xp_pcre_match_count


EXEC master.dbo.xp_pcre_match_count @input, @regex, @result OUTPUT
  • @input is the text to check.
  • @regex is the regular expression.
  • @result is an output parameter that will hold the number of times the regular expression matched the input string (or NULL in the case of NULL inputs and/or an invalid regex).

xp_pcre_match_count tells you how many non-overlapping matches were found in the input string. The reason for making this a separate procedure than xp_pcre_match is for efficiency. In xp_pcre_match, as soon as there is one match, the procedure can return. xp_pcre_match_count needs to continually attempt a match until it reaches the end of the input string.

For example, this will determine how many times a separate series of numbers (of any length) appears in the input:

EXEC master.dbo.xp_pcre_match_count '123abc4567xyz', '\d+', @out OUTPUT
PRINT @out

prints out:


3. xp_pcre_replace


EXEC master.dbo.xp_pcre_replace @input, @regex, @replacement, @result OUTPUT
  • @input is the text to parse.
  • @regex is the regular expression.
  • @replacement is what each match will be replaced with.
  • @result is an output parameter that will hold the result.

xp_pcre_replace is a search-and-replace function. All matches will be replaced with the contents of the @replacement parameter.

For example, this is how you would remove all white space from an input string:

EXEC master.dbo.xp_pcre_replace 'one  two    three four ', '\s+', '', @out OUTPUT
PRINT '[' + @out + ']'

prints out:


To replace all numbers (regardless of length) with "###":

EXEC master.dbo.xp_pcre_replace
   '12345 is less than 99999, but not 1, 12, or 123',
   @out OUTPUT

PRINT @out

prints out:

### is less than ###, but not ###, ###, or ###

Capturing parentheses is also supported. You can then use the captured text in your replacement string by using the variables $1, $2, $3, etc. For example:

EXEC master.dbo.xp_pcre_replace
   'one two three four five six seven',
   '(\w+) (\w+)',
   '$2 $1,',
   @out OUTPUT

PRINT @out

prints out:

two one, four three, six five, seven

If you need to include a literal $ in your replacement string, escape it with a \. Also, if your replacement variable needs to be followed immediately by a digit, you'll need to put the variable number in braces. ${1}00 would result in the first capture followed by the literal characters 00. For example:

EXEC master.dbo.xp_pcre_replace
   '75, 85, 95',
   @out OUTPUT

PRINT @out

prints out:

$7500, $8500, $9500

4. xp_pcre_format


EXEC master.dbo.xp_pcre_format @input, @regex, @format, @result OUTPUT
  • @input is the text to match.
  • @regex is the regular expression.
  • @format is the format string.
  • @result is an output parameter that will hold the result.

xp_pcre_format behaves exactly like Regex.Result() in .NET or string interpolation in Perl (i.e., $formatted_phone_number = "($1) $2-$3")

For example, the regex (\d{3})[^\d]*(\d{3})[^\d]*(\d{4}) will parse just about any US-phone-number-like string you throw at it:


SET @regex = '(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})'

SET @format = '($1) $2-$3'

EXEC master.dbo.xp_pcre_format
   @out OUTPUT
PRINT @out

EXEC master.dbo.xp_pcre_format
   @out OUTPUT
PRINT @out

EXEC master.dbo.xp_pcre_format
   ' 310!555 hey! 1212 hey!',
   @out OUTPUT
PRINT @out

EXEC master.dbo.xp_pcre_format
   ' hello, ( 310 ) 555.1212 is my phone number. Thank you.',
   @out OUTPUT
PRINT @out

prints out:

(310) 555-1212
(310) 555-1212
(310) 555-1212
(310) 555-1212

The capturing and escaping conventions are the same as with xp_pcre_replace.

5. xp_pcre_split


EXEC master.dbo.xp_pcre_split @input, @regex, @column_number, @result OUTPUT
  • @input is the text to parse.
  • @regex is a regular expression that matches the delimiter.
  • @column_number indicates which column to return.
  • @result is an output parameter that will hold the formatted results.

Column numbers start at 1. An error will be raised if @column_number is less than 1. In the event that @column_number is greater than the number of columns that resulted from the split, @result will be set to NULL.

This function splits text data on some sort of delimiter (comma, pipe, whatever). The cool thing about a split using regular expressions is that the delimiter does not have to be as consistent as you would normally expect.

For example, take this line as your source data:

one ,two|three : four

In this case, our delimiter is either a comma, pipe or colon with any number of spaces either before or after (or both). In regex form, that is written: \s*[,|:]\s*.

For example:


SET @input = 'one  ,two|three  : four'

SET @regex = '\s*[,|:]\s*'

EXEC master.dbo.xp_pcre_split @input, @regex, 1, @out OUTPUT
PRINT @out

EXEC master.dbo.xp_pcre_split @input, @regex, 2, @out OUTPUT
PRINT @out

EXEC master.dbo.xp_pcre_split @input, @regex, 3, @out OUTPUT
PRINT @out

EXEC master.dbo.xp_pcre_split @input, @regex, 4, @out OUTPUT
PRINT @out

prints out:


6. xp_pcre_show_cache


EXEC master.dbo.xp_pcre_show_cache

In order to prevent repeated regex recompilation, xp_pcre keeps a cache of the last 50 regular expressions it has processed. (Look at the bottom of RegexCache.h to change this hard-coded value.) xp_pcre_show_cache returns a result set containing all of the regular expressions currently in the cache. There's really no need to use it in the course of normal operations, but I found it useful during development. (I figured I would leave it in since it may be helpful for anyone who is looking at this to learn more about extended stored procedure programming.)

7. fn_pcre_match, fn_pcre_match_count, fn_pcre_replace, fn_pcre_format and fn_pcre_split

These are user-defined functions that wrap the stored procedures. This way you can use the function as part of a SELECT list, a WHERE clause, or anywhere else you can use an expression (like CHECK constraints!). To me, using the UDFs is a much more natural way to use this library.

USE pubs

SELECT master.dbo.fn_pcre_format(
   '($1) $2-$3'
   ) as formatted_phone

This would format every phone number in the "authors" table.

Please note, you'll either need to create the UDFs in every database that you use them in or remember to always refer to them using their fully-qualified names (i.e., master.dbo.fn_pcre_format). Alternatively, you can follow bmoore86's advice at the bottom of this page in his post entitled "You don't have to put the functions in every database".

Also note that user-defined functions in SQL Server are not very robust when it comes to error handling. If xp_pcre returns an error, the UDF will suppress it and will return NULL. If you are using the UDFs and are getting NULLs in unexpected situations, try running the underlying stored procedure. If xp_pcre is returning an error, you'll be able to see it.

8. Installation

  1. Copy xp_pcre.dll into your \Program Files\Microsoft SQL Server\MSSQL\binn directory.
  2. Run the SQL script INSTALL.SQL. This will register the procedures and create the user-defined functions in the master database.
  3. If you'd like to run some basic sanity checks/assertions, run TESTS.SQL and ERROR_TESTS.SQL. These scripts also serve to document the behavior of the procedures in cases of invalid input.

9. Unicode support

Unfortunately, this version does not support Unicode arguments. Potential solutions include:

  1. Use xp_regex. Internally, the CLR and .NET Framework are 100% Unicode. This option is not recommended, however, due to the potential problems with hosting the CLR inside the SQL Server process.
  2. Use the Boost Regex++ library. Unfortunately, this means giving up a lot of the newer regular expression functionality (zero-width assertions, cloistered pattern modifiers, etc.).
  3. Have xp_pcre convert to UTF-8, which is supported by PCRE. Since I don't use Unicode data in SQL Server, I haven't implemented it. We'll leave this as the dreaded "exercise for the reader". :)
  4. Use CAST, CONVERT or implicit conversions in the UDFs to coerce the arguments to ASCII. This probably won't work for you because the reason you're using NVARCHAR/NTEXT columns in the first place is because your data cannot be represented using ASCII.

10. Misc

To build the code, you'll need to have the Boost libraries installed. You can download them from Just change the "Additional Include Directories" entry under the project properties in VS.NET. It's under Configuration Properties | C/C++ | General.

Comments/corrections/additions are welcome. Feel free to email can find my email address in the header of any of the source files. Thanks!

11. History

  • 16 Mar 05 (v. 1.3.1):
    • Added xp_pcre_match_count and fn_pcre_match_count.
  • 20 Feb 05 (v. 1.3):
    • All PCRE++ code was removed and rewritten from scratch. It wasn't thread safe and was too inefficient (in my opinion) when doing splitting and replacing. This should hopefully improve concurrency (since I no longer have to do any locking on the PCRE objects). Also, since I started from scratch, I was able to make the behavior of splitting and replacing/formatting much closer to what Perl produces (especially in cases when there is a zero-width match.)
    • Added xp_pcre_format.
    • Parameter validation and error handling have been improved.
    • Updated TESTS.sql and added ERROR_TESTS.sql.
  • 14 Feb 05 (v. 1.2):
    • Fixed the issue where splitting on a regex that matched a zero-width string (i.e., '\s*') would cause xp_pcre to loop infinitely.
    • Error conditions will now cause the output parameter to be set to NULL. The old version left the value unchanged.
    • Matching using the pcrepp::Pcre objects are now protected by a CRITICAL_SECTION. Although PCRE++ objects can be reused, they don't appear thread-safe. If anyone feels this is adversely affecting scalability, please let me know. We can probably modify the cache to allow multiple instances of the same regular expression.
    • Created TESTS.sql as a way to document/verify expected results in both normal and error conditions.
    • This version statically links against PCRE 5. The previous version used PCRE 4.3 DLL. I built both a Debug (pcre5_d.lib) and a Release (pcre5.lib) version of PCRE. xp_pcre will link against the appropriate version when it is built.
    • Parameter data types and sizes are checked both more rigorously and proactively. Previously, I just waited for a generic failure error when trying to read or write parameter values.
    • If the output parameter cannot hold the entire result, an error message will be returned to SQL Server indicating how large the variable is required to be. The value of the parameter will be set to NULL.
    • catch(...) handlers have been added where applicable to prevent an unhandled exception from propagating back to SQL Server.
  • 6 Oct 03 - Updated ZIP to include xp_pcre.dll. Mentioned the Boost requirement in the Misc section. Cleaned up the documentation a bit.
  • 10 Aug 03 - Initial release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dan Farino
Web Developer
United States United States
No Biography provided

Comments and Discussions

Questionerror: not a valid Win32 application Pin
Member 104399593-Dec-13 19:15
MemberMember 104399593-Dec-13 19:15 
QuestionHow to use xp_pcre_match verify Chinese characters Pin
zlycrazy17-Sep-12 22:39
Memberzlycrazy17-Sep-12 22:39 
GeneralVarchar(MAX) and case sensitivity Pin
Doug Perreault12-Jul-10 7:53
MemberDoug Perreault12-Jul-10 7:53 
QuestionHow to return matched result... Pin
John Skrotzki18-Nov-09 15:21
MemberJohn Skrotzki18-Nov-09 15:21 
GeneralCould not load the DLL xp_pcre.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.). Pin
Venkat1.V18-Aug-09 21:42
MemberVenkat1.V18-Aug-09 21:42 
GeneralExecute permissions (SQL Server 2005) Pin
the-beast25-May-09 23:17
Memberthe-beast25-May-09 23:17 
GeneralDan Merino - Thanks Pin
Aramiel11-Apr-09 11:01
MemberAramiel11-Apr-09 11:01 
QuestionAnyone implemented for Unicode data Pin
batch5813-Jan-09 0:12
Memberbatch5813-Jan-09 0:12 
QuestionText overflow when using nvarchar(max)? Pin
hcaudill23-Oct-08 16:24
Memberhcaudill23-Oct-08 16:24 
GeneralBeginner Needs Help In Installing... Please Help Pin
harissahlan24-Aug-08 22:40
Memberharissahlan24-Aug-08 22:40 
QuestionHow to use SQL functions with replace? Pin
MattPenner18-Mar-08 8:04
MemberMattPenner18-Mar-08 8:04 
NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés1-Mar-08 23:14
MemberPablo Osés1-Mar-08 23:14 
GeneralSplit goes into a loop Pin
Donald Desnoyer20-Apr-07 9:29
MemberDonald Desnoyer20-Apr-07 9:29 
Generalxp_pcre - Regular Expressions in T-SQL Pin
Rich Gruber24-Nov-06 4:21
MemberRich Gruber24-Nov-06 4:21 
Generalinstallation directory for SQL 2005 Pin
leana_ahmed27-Sep-06 6:53
Memberleana_ahmed27-Sep-06 6:53 
GeneralA possible more simple solution for Unicode Pin
Uwe Keim5-Mar-06 18:08
sitebuilderUwe Keim5-Mar-06 18:08 
Questionxp_pcre & sqlsrv 64 bit Pin
FM50009-Nov-05 4:09
MemberFM50009-Nov-05 4:09 
AnswerRe: xp_pcre & sqlsrv 64 bit Pin
Jaspers Yvo15-May-08 1:44
MemberJaspers Yvo15-May-08 1:44 
GeneralRe: xp_pcre & sqlsrv 64 bit Pin
FM500019-May-08 0:01
MemberFM500019-May-08 0:01 
GeneralRe: xp_pcre & sqlsrv 64 bit Pin
Jaspers Yvo17-Oct-08 5:55
MemberJaspers Yvo17-Oct-08 5:55 
AnswerRe: xp_pcre & sqlsrv 64 bit [modified] Pin
Smuus28-May-10 1:10
MemberSmuus28-May-10 1:10 
GeneralRe: xp_pcre & sqlsrv 64 bit [modified] Pin
who_else28-Dec-12 10:16
Memberwho_else28-Dec-12 10:16 
GeneralMatch Count function Pin
Lord_Rat6-Sep-05 11:26
MemberLord_Rat6-Sep-05 11:26 
GeneralRe: Match Count function Pin
VamsiT18-Aug-06 6:16
MemberVamsiT18-Aug-06 6:16 
GeneralProblems with fn_pcre_replace replacements Pin
netshark8610-Aug-05 12:50
Membernetshark8610-Aug-05 12:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.