RegEx helper method to make extracting patterns from strings a cinch

by martin 12. July 2009 15:10

Quite often I find myself trying to create a custom string from the “pattern” found in an input string.  Through trial and error, I have found a real simple and powerful way to do this, and here it is!  It works simply by using the grouping operator “()” for regular expressions and utilizing the power of String.Format.  This implementation is written as a .Net 3.5 extension method for the String class.  I overloaded it to be more flexible; you can pass in RegexOptions, a String representation of a regular expression, or a precompiled Regex object for optimal efficiency.  One important note is that Regex works such that the entire expression is always the first group match (grouping 0) and that all groupings that you explicitly state start at grouping number 1 and enumerate up from there.

StringRegexHelper.cs (1.54 kb)

    1 using System;

    2 using System.Text.RegularExpressions;

    3 

    4 namespace CustomExtensions

    5 {

    6     public static class StringRegexHelper

    7     {

    8         public static String FindAndFormat(this String StringToSearch,

    9             Regex RegularExpressionToSearchFor, String FormattingExpression)

   10         {

   11             String strResult = null;

   12             Match match = RegularExpressionToSearchFor.Match(StringToSearch);

   13             if (match.Success)

   14             {

   15                 String[] arrCaptures = new String[match.Groups.Count];

   16                 for (int i = 0; i < match.Groups.Count; i++)

   17                 {

   18                     arrCaptures[i] = match.Groups[i].Captures[0].Value;

   19                 }

   20                 strResult = String.Format(FormattingExpression, arrCaptures);

   21             }

   22             return strResult;

   23         }

   24 

   25         public static String FindAndFormat(this String StringToSearch,

   26             String RegularExpressionToSearchFor, String FormattingExpression)

   27         {

   28             return FindAndFormat(StringToSearch,

   29                 new Regex(RegularExpressionToSearchFor),

   30                 FormattingExpression);

   31         }

   32 

   33         public static String FindAndFormat(this String StringToSearch,

   34             String RegularExpressionToSearchFor, String FormattingExpression,

   35             RegexOptions RegularExpressionOptions)

   36         {

   37             return FindAndFormat(StringToSearch,

   38                 new Regex(RegularExpressionToSearchFor,

   39                     RegularExpressionOptions),

   40                 FormattingExpression);

   41         }

   42     }

   43 }

Here are 3 examples of how this can be used:

    1 strResult = "myusername-file1.txt".FindAndFormat(@"(\w+)-.",

    2     "{1}@ad.domain.com");

    3 // Returns "myusername@ad.domain.com"

    4 

    5 strResult = "BatchFile072009.txt".FindAndFormat(@"BatchFile(\d\d)(\d\d\d\d)\.txt",

    6     "Batch file for year {2} and month {1} retrieved from {0}");

    7 // Returns "Batch file for year 2009 and month 07 retrieved from BatchFile072009.txt"

    8 

    9 String strInput = "John_Doe;123-45-6789;04/12/2005";

   10 strResult = strInput.FindAndFormat(@"(\w+)_(\w+);(\d{3})-(\d{2})-(\d{4});(\d+)/(\d+)/(\d+)",

   11     "Last name = {2}  First name = {1}  SSN = {3}{4}{5}  DOB = {6}-{7}-{8}");

   12 // Returns "Last name = Doe  First name = John  SSN = 123456789  DOB = 04-12-2005"

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C#

Comments

Add comment


(Will show your Gravatar icon)  

biuquote
  • Comment
  • Preview
Loading



Welcome

Please contact me if you have a great idea for a project and need technical expertise in designing, developing, or integrating a custom software solution. 

Recent Comments

Comment RSS