Slightly boring class for doing some regex... I've stored my notes on my pWord program which I also use for tracking passwords and it will always be open source. www.sourceforge.net/projects/pword . Please feel free to ignore my IE10 formatting woes using the WYSIWYG!.
using System;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
using System.Data;
using System.Data.SqlTypes;
using System.Text;
namespace RegEx {
public
class RX {
[SqlFunction]
static
bool IsMatch(String input, String pattern) {
// Regex.Match()
MatchCollection mc1 = Regex.Matches(input, pattern);
if (mc1.
Count
> 0)
return
true
;
else
false
}
int
GetMatchCount(String input, String pattern) {
mc1.
String GetMatch(String input, String pattern)
{
MatchCollection mc1 = Regex.Matches(input, pattern, RegexOptions.Compiled);
String
output
=
""
foreach (Match m1
in
mc1)
= m1.ToString();
//
only
find the
first
occurrence ;)
break;
String GetAllMatches(String input, String pattern)
StringBuilder
= new StringBuilder();
.Append(
);
.Append( m1.ToString());
.ToString();
Check out the SQL Script for adding UDFs into SQL Server 2012. This entry participated in the Technology Guru TechNet WiKi for May contest.
Naomi N edited Revision 9. Comment: Added link to Contest
isenthil edited Revision 6. Comment: Added the code formatting
Marc Noon edited Revision 3. Comment: Trying to fix background
The code is barely visible. Is it possible to adjust the code to this article?
I believe the reason its barely visible is b/c the background on vs2012 is b/c the Visual Experience theme is dark. The background color is supposed to be a charcoal color and it didn't stick for some reason.
I suggest to post this code using Code button here in the TechNet entry. If you want I can fix it for you.
Naomi thanks that would be fabulous.
Looks like you already fixed it - it is much better now.
BTW, for the code above I think you may also want to show some T-SQL samples of using this CLR.
I created two posts that relate and I need to link them together. Having a little trouble with the editor. thx
Revision: edited tags
I'm planning on adding some non-career ending examples soon.