packages/Windows API Code Pack 1.1/source/Samples/DirectX/CS/UtilitiesLibrary/MeshLoader/XMeshLoader.XDataObject.cs
/// <summary>
/// Processes the given text using the given regex. For every match, the text
/// corresponding to that match is removed from the input text, and the <paramref name="processGroup"/>
/// delegate is invoked to obtain whatever object instance corresponds to the
/// matched text.
/// </summary>
/// <typeparam name="T">The type of object that will be returned for any matched text</typeparam>
/// <param name="inputText">The text to parse. On return, this will reference to a new string containing only the text that was not parsed into new objects</param>
/// <param name="regex">The regex expression to use to match text</param>
/// <param name="processGroup">The delegate invoked for any matching text, and which returns a new object instance corresponding to the matched text</param>
/// <returns>An enumeration of the objects created by parsing the text</returns>
Processes the given text using the given regex. For every match, the text
corresponding to that match is removed from the input text, and the
delegate is invoked to obtain whatever object instance corresponds to the
matched text.
Processes the given text using the given regex . For every match the text corresponding to that match is removed from the input text and the delegate is invoked to obtain whatever object instance corresponds to the matched text .
private static IEnumerable<T> ExtractByRegex<T>(ref string inputText,
                Regex regex, Func<GroupCollection, T> processGroup)
            {
                StringBuilder bodyBuilder = null;
                List<T> dataObjects = new List<T>();
                Match matchObject = regex.Match(inputText);
                int indexCopy = 0;
                while (matchObject.Success)
                {
                    if (bodyBuilder == null)
                    {
                        bodyBuilder = new StringBuilder(inputText.Length - matchObject.Length);
                    }
                    bodyBuilder.Append(inputText.Substring(indexCopy, matchObject.Index - indexCopy));
                    indexCopy = matchObject.Index + matchObject.Length;
                    dataObjects.Add(processGroup(matchObject.Groups));
                    matchObject = matchObject.NextMatch();
                }
                if (bodyBuilder != null)
                {
                    bodyBuilder.Append(inputText.Substring(indexCopy));
                    inputText = bodyBuilder.ToString();
                }
                return dataObjects;
            }
private static IEnumerable < T > ExtractByRegex < T > ( ref string inputText , Regex regex , Func < GroupCollection , T > processGroup ) { StringBuilder bodyBuilder = null ; List < T > dataObjects = new List < T > ( ) ; Match matchObject = regex . Match ( inputText ) ; int indexCopy = 0 ; while ( matchObject . Success ) { if ( bodyBuilder == null ) { bodyBuilder = new StringBuilder ( inputText . Length - matchObject . Length ) ; } bodyBuilder . Append ( inputText . Substring ( indexCopy , matchObject . Index - indexCopy ) ) ; indexCopy = matchObject . Index + matchObject . Length ; dataObjects . Add ( processGroup ( matchObject . Groups ) ) ; matchObject = matchObject . NextMatch ( ) ; } if ( bodyBuilder != null ) { bodyBuilder . Append ( inputText . Substring ( indexCopy ) ) ; inputText = bodyBuilder . ToString ( ) ; } return dataObjects ; }
Processes the given text using the given regex.
Processes the given text using the given regex .
// Deferring creation of the StringBuilder has a couple // of beneficial effects: the buffer can be pre-sized to // a likely reasonable size; more importantly, the code can // avoid making a copy of the original input if no sub-objects // were found.
An enumeration of the objects created by parsing the text
An enumeration of the objects created by parsing the text
string
ref
regex
Regex
The regex expression to use to match text
The regex expression to use to match text
processGroup
Func<GroupCollection, T>
The delegate invoked for any matching text, and which returns a new object instance corresponding to the matched text
The delegate invoked for any matching text and which returns a new object instance corresponding to the matched text
inputText
The text to parse. On return, this will reference to a new string containing only the text that was not parsed into new objects
The text to parse . On return this will reference to a new string containing only the text that was not parsed into new objects
typeparam
The type of object that will be returned for any matched text
The type of object that will be returned for any matched text