Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

Previously I've posted a few Helper Classes . This post describes my RegexMatchsHelper class

I also added a few related links:

Use Regular Expressions to Constrain Input in ASP.NET

using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
 
      ///<summary>
      /// Summary description for RegexMatchs.
      ///</summary>
      public class RegexMatchsHelper
      {
            public RegexMatchsHelper()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }
 
//          public static void RetrieveMatchedStringTest()
//          {
//                string sResp=StreamHelper.FileToString("searchManyPages.htm");
//                string pattern="tokenKey=(.*?)&target=results_ResultsList";
//                string sRet=RetrieveMatchedString( sResp, pattern);  
//          }
            //assumed that pattern has something like (.*?) inside
        public static string CaptureFirstUnnamedGroup(string input, string pattern)
        {
            return CaptureFirstUnnamedGroup(input, pattern, RegexOptions.None);
        }
        public static string CaptureFirstUnnamedGroupIgnoreNL(string input, string pattern)
        {
            return CaptureFirstUnnamedGroupIgnoreNL(input, pattern, RegexOptions.None);
        }
        public static string CaptureFirstUnnamedGroupIgnoreNL(string input, string pattern, RegexOptions options)
        {
            //from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/regexnet.asp
            // SingleLine Has nothing to do with how many lines are in the input string. Rather, will cause the . (period) metacharacter to match any character, instead of any character except \n, which is the default.
            return CaptureFirstUnnamedGroup(input, pattern, options | RegexOptions.Singleline);
        }
        public static string CaptureFirstUnnamedGroup(string input, string pattern, RegexOptions options)
            {
                  string sRet="";
            Regex regex = new Regex(pattern, options);
                  Match m = regex.Match(input);
                  if((m.Success) )
                  {
                        if ((m.Groups.Count>=2) && (m.Groups[1].Captures.Count>0))
                               sRet= m.Groups[1].Captures[0].Value;
                  }
                  FSHelperLib.DebugHelper.TracedLine(" captured " + sRet );
                  return sRet;
            }
        ///<summary>
        /// Returns array of matched strings
        ///</summary>
        ///<param name="input"></param>
        ///<param name="pattern"></param>
        ///<param name="options"></param>
        ///<returns></returns>
        public static string[] MultipleMatchesFirstUnnamedGroupCaptures(string input, string pattern, RegexOptions options)
        {
            //string sRet = "";
            Regex regex = new Regex(pattern, options);
            MatchCollection matches = regex.Matches(input);
            string[] asCaptures = new string[matches.Count];
            int i = 0;
            foreach (Match m in matches)
            {
                Debug.Assert(((m.Groups.Count == 2) && (m.Groups[1].Captures.Count > 0)));
                asCaptures[i] = m.Groups[1].Captures[0].Value;
               // FSHelperLib.DebugHelper.TracedLine(" captured " + sRet);
                i++;
            }
            return asCaptures;
        }
 
    }
 
Posted on Wednesday, June 6, 2007 12:16 AM | Back to top


Comments on this post: My RegexMatchsHelper class

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net