Click here to Skip to main content
Click here to Skip to main content

xp_pcre - Regular Expressions in T-SQL

By , 16 Mar 2005
 

Introduction

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 www.pcre.org. (You don't need to download the PCRE library in order to use xp_pcre. The library is statically linked.)

Overview

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

Syntax:

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:

DECLARE @out CHAR(1)
EXEC master.dbo.xp_pcre_match 'abc123xyz', '\d{2,}', @out OUTPUT
PRINT @out

prints out:

1

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

DECLARE @out CHAR(1)
EXEC master.dbo.xp_pcre_match 'abc123xyz', '^\d{2,}$', @out OUTPUT
PRINT @out

prints out:

0

2. xp_pcre_match_count

Syntax:

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:

DECLARE @out VARCHAR(20)
EXEC master.dbo.xp_pcre_match_count '123abc4567xyz', '\d+', @out OUTPUT
PRINT @out

prints out:

2

3. xp_pcre_replace

Syntax:

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:

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

prints out:

[onetwothreefour]

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

DECLARE @out VARCHAR(8000)
EXEC master.dbo.xp_pcre_replace
   '12345 is less than 99999, but not 1, 12, or 123',
   '\d+',
   '###',
   @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:

DECLARE @out VARCHAR(8000)
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:

DECLARE @out VARCHAR(8000)
EXEC master.dbo.xp_pcre_replace
   '75, 85, 95',
   '(\d+)',
   '\$${1}00',
   @out OUTPUT

PRINT @out

prints out:

$7500, $8500, $9500

4. xp_pcre_format

Syntax:

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:

DECLARE @out VARCHAR(100)

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

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

EXEC master.dbo.xp_pcre_format
   '(310)555-1212',
   @regex,
   @format,
   @out OUTPUT
PRINT @out

EXEC master.dbo.xp_pcre_format
   '310.555.1212',
   @regex,
   @format,
   @out OUTPUT
PRINT @out

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

EXEC master.dbo.xp_pcre_format
   ' hello, ( 310 ) 555.1212 is my phone number. Thank you.',
   @regex,
   @format,
   @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

Syntax:

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:

DECLARE @out VARCHAR(8000)

DECLARE @input VARCHAR(50)
SET @input = 'one  ,two|three  : four'

DECLARE @regex VARCHAR(50)
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:

one
two
three
four

6. xp_pcre_show_cache

Syntax:

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
GO

SELECT master.dbo.fn_pcre_format(
   phone,
   '(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})',
   '($1) $2-$3'
   ) as formatted_phone
FROM
   authors

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 www.boost.org. 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 me...you 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.

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to use xp_pcre_match verify Chinese characters [modified]memberzlycrazy17 Sep '12 - 22:39 
How to use xp_pcre_match verify Chinese characters
 
EXEC master.dbo.xp_pcre_replace 'an一', '^[\u4e00-\u9fa5]*', '|', @out OUTPUT
 
消息 20001,级别 16,状态 1,第 0 行
pcre_compile() failed: PCRE does not support \L, \l, \N, \U, or \u at character 3

modified 18 Sep '12 - 5:10.

GeneralVarchar(MAX) and case sensitivitymemberDoug Perreault12 Jul '10 - 7:53 
I'd like to use varchar(max) for the input and output parameters in the function call to xp_pcre_replace, but that doesn't seem to work. I get the message:
 
Msg 20001, Level 16, State 1, Line 0
Invalid data type on parameter 4 (should be CHAR or VARCHAR)
 
when I run the stored procedure directly.
 
Also, I couldn't find anything in your notes or in the comments regarding global replacement, case sensitivity, etc. What are the rules on those modifiers? Or can't I use them?
QuestionHow to return matched result...memberJohn Skrotzki18 Nov '09 - 15:21 
I am trying to determine if there are invalid characters in a string.
 
I am using match to determine if there are invalid characters: [^a-zA-Z0-9\_\-] -- these are the ok characters, anything else a no no.
 
Good so far, but I would like to go a step further and actually return the invalid characters and for the life of me I cannot figure it out. Is it possible with this extension???
 
Thanks...
GeneralCould not load the DLL xp_pcre.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).memberVenkat1.V18 Aug '09 - 21:42 
hi dan,
i had tried the new one which u have mentioned in the earlier posts. even then sql is throwing some error stating the module could not be found.
 
Could not load the DLL xp_pcre.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found. Frown | :(
 
Please Dan, help me out in this issue. wil be really thankful if it gets solved.
 
its really urgent D'Oh! | :doh:
GeneralExecute permissions (SQL Server 2005)memberthe-beast25 May '09 - 23:17 
Since these stored procedures live in the 'master' table you probably don't want to grant users full privileges to the 'master' table just to execute the regular expressions. To limit the DB user's to just execute permissions on the 'master' table:
 
Run the following SQL:
 
/* CREATE A NEW ROLE */
CREATE ROLE db_executor
 
/* GRANT EXECUTE TO THE ROLE */
GRANT EXECUTE TO db_executor
 
Then via the SQL Management Studio program go to 'Security -> Roles' in the 'master' table. Go to the 'Properties' of the 'db_executor' role and add the DB user account(s) you want to use to the 'Role Members' section.
 
That's it. Hope this helps.
 
P.S. Execute code from http://www.sqldbatips.com/showarticle.asp?ID=8. This link also has code for SQL Server 2000.
GeneralDan Merino - ThanksmemberAramiel11 Apr '09 - 11:01 
Gracias ...!, tu source code me fue de mucha ayuda. Smile | :) Thumbs Up | :thumbsup: Smile | :)
QuestionAnyone implemented for Unicode datamemberbatch5813 Jan '09 - 0:12 
I am using xp_pcre.dll for the find and replace purpose and is working fine. But now,i need to support for the unicode data as well.
 
Can we convert the xp_pcre.dll to support unicode ? how and what modification is to be carried out...
 
Is there Anyone who have implemented the xp_pcre.dll for the unicode data?
Please do let me know the details procedure to convert xp_pcre.dll for unicode data.
 
i want a get to the perfection in computer world

QuestionText overflow when using nvarchar(max)?memberhcaudill23 Oct '08 - 16:24 
I've run into a strange situation that seems to involve long text overflowing. I'm using fn_pcre_replace on an nvarchar(max) column, and when I replace with more characters than were there originally, it returns NULL if the original string was over 8000 characters long.
 
For example, this
 
Select master.dbo.fn_pcre_replace(Overview,'a', 'X')
from ProjectContent
works as expected, but this
 
Select master.dbo.fn_pcre_replace(Overview,'a', 'XXX')
from ProjectContent
returns NULL in most cases (apparently, wherever Len(Overview)>8000).
 
Is this a limitation of xp_pcre, or is there something I can do in my SQL to make this work?
GeneralBeginner Needs Help In Installing... Please Helpmemberharissahlan24 Aug '08 - 22:40 
Hi,
 
I'm a beginner in programming. I have run INSTALL.SQL and it was successful, but when I ran the TEST.SQL, the result table shows that most of the status are 'FAILED!!!'. I also receive some error messages when I run ERROR_TESTS.SQL. The messages are the same which is 'Msg 17750, Level 16, State 0, Procedure xp_pcre_split, Line 1
Could not load the DLL xp_pcre.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.)'. Any idea what's wrong? For additional info: the is no '\Program Files\Microsoft SQL Server\MSSQL\binn' directory in my computer. I put the dll file in 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\binn'. Could this be the problem?
 
Thanks in advance.
 
Haris
QuestionHow to use SQL functions with replace?memberMattPenner18 Mar '08 - 8:04 
We have a library system. All 3 character strings in the Call Number should be uppercase. For instance, FIC for fiction, REF for reference, etc. These are user entered values so naturally there are several lowercase values.
 
I want to change all 3 character strings in this field to uppercase.
 
I can select all 3 character strings by using:
master.dbo.fn_pcre_match(callnumber, '^\d{3}$|^\d{3}[^\d]|[^\d]\d{3}$') = 1
 
However, how do I use replace to change these to uppercase? What I want to do is something like:
master.dbo.fn_pcre_replace(callnumber, '^(\d{3})$', UPPER('$1'))
 
What's the best way to do this?
 
Thanks!
Matt
NewsThere's a new version of the RegEx Tester Tool !memberBucanerO_Slacker1 Mar '08 - 23:14 
I have released a new version of the RegEx Tester tool. You can download it free from http://www.codeproject.com/KB/string/regextester.aspx and http://sourceforge.net/projects/regextester
 
With RegEx Tester you can fully develop and test your regular expression against a target text. It's UI is designed to aid you in the RegEx developing. It uses and supports ALL of the features available in the .NET RegEx Class.
GeneralSplit goes into a loopmemberDonald Desnoyer20 Apr '07 - 9:29 

In testing the code I found this statement caused the xp to go into a loop and I had to stop the SQL service to get out of it.
 
select dbo.fn_pcre_split('asdfs|,sdfsdfsdbsdfsdf,sdfsdfc,sdfsdd', '\s*|,\s*', 2)
 
I'm not an expert on RegEx's but I know enough to know that loops on xp's called from T-SQL are not a good thing.
 
Perhaps someone with more experience could look into this ?
 
Thanks,
Don
Generalxp_pcre - Regular Expressions in T-SQLmemberRich Gruber24 Nov '06 - 4:21 
This is outstanding! Thank you.
Generalinstallation directory for SQL 2005memberleana_ahmed27 Sep '06 - 6:53 
I'm running MS SQL 2005 on an XP box. Initially I tried the installation directory listed but it wasn't working.
Then I tried: C:\Program Files\Microsoft SQL Server\90\Tools\Binn
and it worked like a charm Big Grin | :-D
 
Leana
GeneralA possible more simple solution for UnicodesitebuilderUwe Keim5 Mar '06 - 18:08 
Regarding your comments on not being unicode-enabled, you could use the GRETA library from Microsoft which supports Unicode and is Perl 5 compatible:
 
http://research.microsoft.com/projects/greta/ [^]
 
--
Try our Windows-based CMS: www.zeta-producer.com
See me working: www.magerquark.com
Questionxp_pcre & sqlsrv 64 bitmemberFM50009 Nov '05 - 4:09 
hi,
 
is it possible to install the dll on a 64 bit sqlserver cluster?
 
Greets Frank
AnswerRe: xp_pcre & sqlsrv 64 bitmemberJaspers Yvo15 May '08 - 1:44 
Hello,
I have the same problem.
 
Did you succeed at the end ?
 
Kind regards,
 
Yvo Jaspers
Ex Arte NV
GeneralRe: xp_pcre & sqlsrv 64 bitmemberFM500019 May '08 - 0:01 
Hi Yvo,
 
due to the fact that nobody responded to my question, I also no longer be pursued.
It was too risky for me, to test that dll on a production system (I've no testing 64bit Cluster).
 
Greetings Frank
GeneralRe: xp_pcre & sqlsrv 64 bitmemberJaspers Yvo17 Oct '08 - 5:55 
Hello Frank,
 
It has been a long time and I wonder if you have figured this out.
I am still looking for a solution for xp_pcre.dll on a 64bit OS.
 
Kind regards,
 
Yvo
AnswerRe: xp_pcre & sqlsrv 64 bit [modified]memberSmuus28 May '10 - 1:10 
Yes it is possible, as I just got there. It was a bit of a headache though.
 
Had to recompile PCRE version 8 (i didn't get version 5 to compile) by using the cmake script and then compiling it with Visual Studio C++ 2008 with the x64 option.
I also needed the boost framework.
And then the source files provided with this article.
 
You need to copy the pcre.lib and rename it to pcre5.lib, since xp_pcre needs version 5 (it is actually 8, but who cares...)
You need to copy the boost framework to a folder which is searched by the building system.
You need to CHANGE the lines in PcreRegex.h as follows:
//pcre_free(m_pcre);
free(m_pcre);
//pcre_free(m_pcreExtra);
free(m_pcreExtra);
The code above (free instead of pcre_free) i have from https://www.modsecurity.org/tracker/browse/MODSEC-138[^] where you will see, that this actually MIGHT be a memory problem, but again: who cares.
 
Then it should compile. There is one difference to the x86 version: You need the xp_pcre.dll AND ALSO the pcre.dll from the PCRE-compilation to copy them into the MSSQL\Binn directory.
 
For a quick documentation on the cmake thing look at: http://monetdb.cwi.nl/MonetDB/Documentation/Windows-Installation.html[^] Under 1.46.
 
It is tested and running with SQL Server 2008.
 
Phew!
 
Greetings,
Thomas
 
!!! UPDATE !!!
Last month we encountered severe problems with the recompiled prce-dlls. SQL Server eventually crashes when using regex intensively. Best workaround: Write your own CLR DLL.

modified on Thursday, December 9, 2010 8:58 AM

GeneralRe: xp_pcre & sqlsrv 64 bit [modified]memberwho_else28 Dec '12 - 10:16 
Not sure if it helps but I was able to compile a 64 bit version of xp_pcre by linking the project to the lib files from
pcre8 for windows], I just set the project to Release/x64 and copied opends60 from the sql client sdk and it all built without a problem on VS2008.
 
NOTE: I did have boost pre-compiled and installed.
 
Preliminary tests shows it is working, will find out how it handles load (normally process 3-4M records per day on the 32bit sql server, migrating to 64 if the load test works.
GeneralMatch Count functionmemberLord_Rat6 Sep '05 - 11:26 
The Match Count function sometimes returns the varchar value '*' which causes my Sql statements to fail when I attempt to cast that in to a INT column.
 
I narrowed it down to the return type included in the INSTALL.SQL file.
 
I had to modify the following function:
 
CREATE FUNCTION [system_function_schema].[fn_regex_match_count]
(
@input VARCHAR(8000), -- the input text
@regex VARCHAR(8000) -- a regular expression
)
RETURNS CHAR(1) AS
BEGIN
DECLARE @out VARCHAR(20)
EXEC master.dbo.xp_pcre_match_count @input, @regex, @out OUT
 
RETURN CAST(@out AS INT)
END
 

 
to this:
 
CREATE FUNCTION [system_function_schema].[fn_regex_match_count]
(
@input VARCHAR(8000), -- the input text
@regex VARCHAR(8000) -- a regular expression
)
RETURNS INT AS
BEGIN
DECLARE @out VARCHAR(20)
EXEC master.dbo.xp_pcre_match_count @input, @regex, @out OUT
 
RETURN CAST(@out AS INT)
END

GeneralRe: Match Count functionmemberVamsiT18 Aug '06 - 6:16 
Looks like the extended SP xp_pcre_match_count will return an actual count only if the count is less than 10... Any more, and it returns a '*'
 
Unfortunate :-/
GeneralProblems with fn_pcre_replace replacementsmembernetshark8610 Aug '05 - 12:50 
I am trying to use fn_pcre_replace against a text column. I am using a class to match against and putting the match in parenthesis so I can use it in the replacement string...
 
my problem is I cant seem to concatenate the matched value $1 with a bunch of other stuff like other functions outside of quotes:
 
e.g.
 
select master.dbo.fn_pcre_replace(MyColumn,'([\x80-\xFF])','('+str(ascii('$1'))+')')
 
in this example the '$1' is handled as a literal (not replaced) and the ascii function is run against $ instead of the value of $1
 
without the quotes: '('+str(ascii($1))+')'
"Implicit conversion from data type money to varchar is not allowed..." ridiculous
 
even this is not handled properly:
ascii('$1')
the $1 is never expanded WTF | :WTF:
 
I am stuck here.. I need to use a function (i can think of several others) against the matched value or string, but I need the match expanded first.
 
Am I missing something obvious?
 
Regards,
Michael
 

GeneralRe: Problems with fn_pcre_replace replacementsmemberDan Farino10 Aug '05 - 13:08 
The replacement parameter is a literal string, so there is no support for operating on the replacement value _prior_ to placing it into the result string.
 
It looks like you're trying replace high-order ASCII with the numeric ordinal value. I don't think this is possible with a pure regex solution. A special case is Perl, which has the /e modifier that allows programmatic expressions to be used as the replacement, but the PCRCE library doesn't support this as there is no Perl interpreter actually running.
 
You may have to resort to an iterative solution on this one.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 17 Mar 2005
Article Copyright 2003 by Dan Farino
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid