Recipe10.11.Finding a Particular Occurrence of a Match


Recipe 10.11. Finding a Particular Occurrence of a Match

Problem

You need to find a specific occurrence of a match within a string. For example, you want to find the third occurrence of a word or the second occurrence of a social security number. In addition, you may need to find every third occurrence of a word in a string.

Solution

To find a particular occurrence of a match in a string, simply subscript the array returned from Regex.Matches:

 public static Match FindOccurrenceOf(string source, string pattern,                                      int occurrence) {     if (occurrence < 1)     {         throw (new ArgumentException("Cannot be less than 1",                                      "occurrence"));     }     // Make occurrence zero-based.     --occurrence;     // Run the regex once on the source string.     Regex RE = new Regex(pattern, RegexOptions.Multiline);     MatchCollection theMatches = RE.Matches(source);     if (occurrence >= theMatches.Count)     {         return (null);     }     else     {         return (theMatches[occurrence]);     } } 

To find each particular occurrence of a match in a string, build a List<Match> on the fly:

 public static List<Match> FindEachOccurrenceOf(string source, string pattern,                                                       int occurrence) {     List<Match> occurrences = new List<Match>();     // Run the regex once on the source string.     Regex RE = new Regex(pattern, RegexOptions.Multiline);     MatchCollection theMatches = RE.Matches(source);     for (int index = (occurrence - 1); index < theMatches.Count; index  += occurrence)     {         occurrences.Add(theMatches[index]);     }          return (occurrences); } 

The following method shows how to invoke the two previous methods:

 public static void TestOccurrencesOf() {     Match matchResult = FindOccurrenceOf                            ("one two three one two three one two three one"                            + " two three one two three one two three", "two", 2);     if (matchResult != null)         Console.WriteLine(matchResult.ToString() + "\t" + matchResult.Index);     Console.WriteLine();     List<Match> results = FindEachOccurrenceOf                                  ("one one two three one two three one "                                   + " two three one two three", "one", 2);     foreach (Match m in results)         Console.WriteLine(m.ToString() + "\t" + m.Index); } 

Discussion

This recipe contains two similar but distinct methods. The first method, FindOccurrenceOf, returns a particular occurrence of a regular expression match. The occurrence you want to find is passed in to this method via the occurrence parameter. If the particular occurrence of the match does not existfor example, you ask to find the second occurrence, but only one occurrence existsa null is returned from this method. Because of this, you should check that the returned object of this method is not null before using that object. If the particular occurrence exists, the Match object that holds the match information for that occurrence is returned.

The second method in this recipe, FindEachOccurrenceOf, works similarly to the FindOccurrenceOf method, except that it continues to find a particular occurrence of a regular expression match until the end of the string is reached. For example, if you ask to find the second occurrence, this method would return a List<Match> of zero or more Match objects. The Match objects would correspond to the second, fourth, sixth, and eighth occurrences of a match and so on until the end of the string is reached.

See Also

See the ".NET Framework Regular Expressions" and "ArrayList Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net