RegEx Class

RegEx Class

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]        
public static bool IsMatch(String input, String pattern)         {
//            Regex.Match()            
    MatchCollection mc1 = Regex.Matches(input, pattern);            
    if (mc1.Count > 0)                
        return true;            
    else                
    return false;        
}        
 
[SqlFunction]        
public static int GetMatchCount(String input, String pattern)         {            
//            Regex.Match()            
    MatchCollection mc1 = Regex.Matches(input, pattern);            
    return mc1.Count;        
}        
 
[SqlFunction]        
public static String GetMatch(String input, String pattern)        
{            
    MatchCollection mc1 = Regex.Matches(input, pattern, RegexOptions.Compiled);            
    String output = "";            
    if (mc1.Count > 0)            
    {                
        foreach (Match m1 in mc1)                
        {                    
            output = m1.ToString();
            // only find the first occurrence ;)                    
            break;                
        }                
     
    return output;            
    }            
    else                
        return "";        
}        
 
[SqlFunction]        
public static String GetAllMatches(String input, String pattern)        
{            
    MatchCollection mc1 = Regex.Matches(input, pattern, RegexOptions.Compiled);            
    StringBuilder output = new StringBuilder();            
    output.Append("");            
 
    if (mc1.Count > 0)            
    {                
        foreach (Match m1 in mc1)                
        {                    
            output.Append( m1.ToString());
            // only find the first occurrence ;)                
        }                
    return output.ToString();            
    }            
    else               
         return "";        
    }    
}
}

 

 
Check out the SQL Script for adding UDFs into SQL Server 2012.

This entry participated in the Technology Guru TechNet WiKi for May contest. 

 



See Also


Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • 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

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • 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.

  • Marc Noon edited Revision 3. Comment: Trying to fix background

  • 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

  • isenthil edited Revision 6. Comment: Added the code formatting

  • Revision: edited tags

  • I'm planning on adding some non-career ending examples soon.  

  • Naomi  N edited Revision 9. Comment: Added link to Contest

Page 1 of 1 (12 items)