Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have excel file names formatted in to a specific template

ABC_V3_Weekly_Abcdef_from_NOV_05_to_NOV_12


Output should like this

FromDate : 05/09/2014
ToDate : 12/09/2014

2014 is System Year
Posted
Updated 7-Dec-14 22:18pm
v2
Comments
syed shanu 8-Dec-14 4:15am    
where do you get this 2014 .Do you use the Current Year here.
Gihan Liyanage 8-Dec-14 4:17am    
System Year

Try:
(?<frommonth>\w\w\w)(?:_)(?<fromday>\d\d)(?:_to_)(?<tomonth>\w\w\w)(?:_)(?<today>\d\d)
Then Convert the month groups to numbers and include the date from DateTime.Now

It isn't practical to do the whole thing with a regex - it's a Text processor, not a date processor! It's a lot simpler (and more maintainable) to do the info extraction with a Regex, then process that in your preferred language.

[edit]Typo, and spurious HTML closing tags...[/edit]
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 8-Dec-14 4:58am    
The editor messed with your regex code...
OriginalGriff 8-Dec-14 5:08am    
Thanks! (Stoopid software... :laugh:)
Kornfeld Eliyahu Peter 8-Dec-14 5:12am    
Beware - hamsters are reading it as you type!!!
OriginalGriff 8-Dec-14 5:18am    
It's OK - I have a fully trained Attack Cat!
Kornfeld Eliyahu Peter 8-Dec-14 5:20am    
http://s1.ibtimes.com/sites/www.ibtimes.com/files/styles/v2_article_large/public/2013/04/02/man-sues-cat-owner.jpg?itok=I7JSaxtc
This is one way to do it.
C#
string input = "ABC_V3_Weekly_Abcdef_from_NOV_05_to_NOV_12";
Regex regex = new Regex(@"from_(?<from_month>[A-Z]{3})_(?<from_day>[0-9]{2})_to_(?<to_month>[A-Z]{3})_(?<to_day>[0-9]{2})", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
Match m = regex.Match(input);
if (m.Success)
{
    int year = 2014;   // You need to fix this part
    DateTime fromDate = new DateTime(year, int.Parse(m.Groups["from_month"].Value), int.Parse(m.Groups["from_day"].Value);
    DateTime toDate = new DateTime(year, int.Parse(m.Groups["to_month"].Value), int.Parse(m.Groups["to_day"].Value);

string from = String.Format("FromDate : {0}", fromDate.ToString("dd/MM/yyyy"));
string to = String.Format("ToDate : {0}", toDate.ToString("dd/MM/yyyy"));
}
 
Share this answer
 
v3
Check this hope this will help you.

C#
String sval = "ABC_V3_Weekly_Abcdef_from_NOV_05_to_NOV_12";
           int Startvale = sval.IndexOf("_from_") + "_from_".Length;
          int EndValue = sval.IndexOf("_to");
          String FinalString = sval.Substring(Startvale, EndValue - Startvale);
             int monthInDigit = DateTime.ParseExact(FinalString.Substring(0, 3), "MMM", CultureInfo.InvariantCulture).Month;
             String result = FinalString.Substring(4, 2) + "/" + monthInDigit.ToString() + "/" + DateTime.Now.ToString("yyyy");
          MessageBox.Show(result.ToString())



          String result1 = sval.Substring(sval.Length-6);//Betweenas(sval, "_to_", "_to");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900