Recipe 10.10. Returning the Entire Line in Which a Match Is FoundProblemYou have a string or file that contains multiple lines. When a specific character pattern is found on a line, you want to return the entire line, not just the matched text. SolutionUse the StreamReader.ReadLine method to obtain each line in a file in which to run a regular expression against, as shown in Example 10-10. Example 10-10. Returning the entire line in which a match is found
The following method shows how to call the GetLines method with either a filename or a string: public static void TestGetLine() { // Get each line within the file TestFile.txt as a separate string. Console.WriteLine(); List<string> lines = GetLines(@"C:\TestFile.txt", "\n", true); foreach (string s in lines) Console.WriteLine("MatchedLine: " + s); // Get the lines matching the text "Line" within the given string. Console.WriteLine(); lines = GetLines("Line1\r\nLine2\r\nLine3\nLine4", "Line", false); foreach (string s in lines) Console.WriteLine("MatchedLine: " + s); } DiscussionThe GetLines method accepts three parameters:
This method returns a List<string> of strings that contains each line in which the regular expression match was found. The GetLines method can obtain the lines on which matches occur, within a string or a file. When running a regular expression against a file with a name that is passed in to the source parameter (when isFileName equals true) in the GetLines method, the file is opened and read line by line. The regular expression is run against each line and, if a match is found, that line is stored in the matchedLines List<string>. Using the ReadLine method of the StreamReader object saves you from having to determine where each line starts and ends. Determining where a line starts and ends in a string requires some work, as you shall see. Running the regular expression against a string passed in to the source parameter (when isFileName equals false) in the GetLines method produces a MatchCollection. Each Match object in this collection is used to obtain the line on which it is located in the source string. The line is obtained by starting at the position of the first character of the match in the source string and moving one character to the left until either an '\n' character is found or the beginning of the source string is found (this code is found in the GetBeginningOfLine method). This gives you the beginning of the line, which is placed in the variable LineStartPos. Next, the end of the line is found by starting at the last character of the match in the source string and moving to the right until either an '\n' character is found or the end of the source string is found (this code is found in the GetEndOfLine method). This ending position is placed in the LineEndPos variable. All of the text between the LineStartPos and LineEndPos will be the line in which the match is found. Each of these lines is added to the matchedLines List<string> and returned to the caller. Something interesting you can do with the GetLines method is to pass in the string "\n" in the pattern parameter of this method. This trick will effectively return each line of the string or file as a string in the List<string>. Note that if more than one match is found on a line, each matching line will be added to the List<string>. See AlsoSee the ".NET Framework Regular Expressions," "FileStream Class," and "Stream-Reader Class" topics in the MSDN documentation. |