regular expression in c# - capture pattern jan10 or jan08 but not janis
Posted on 2007-11-27
I have descriptions that contain strings like:
"Jan09 blah blah blah"
"blah blah feb12 blah"
Basically the string may contain a token in the form MMMYY where MMM is a three letter month abbrieviation, and YY is a two digit year. This token form may appear 0-2 times in the string (if it appears twice, there will be two months listed, ie apr07-oct07)
I need, for each string, to determine which month appears in the string - if any. I want to write a c# method that will take the string and return either null or the three character month code.
So, if jan07 appears, or jan14 appears, I want it to return jan. But if some other value that is missing the year digits appears, such as "janis feb08" - the test should not return feb. If two months appear, I want the method to return the first month code - "blah apr07-oct07 blah blah" should have a value of "apr"
I figure I need a sequence of regular expressions that test for each month code in sequence - and, if found, note the index location it was found at. If more than one was found, return the one that has the smaller index location value.
So, I need a c# regular expression test that will return true for both "jan08" and "jan12" , but false for "janis" -- and some way to determine the index it was found at, so I'd get an index location value of 11 and not 0 for this string: "janis blah jan08" (if I counted it right).
Or any other code that will get the job done.