使用regx从文件名中提取月份和日期 [英] Extract Month and Date from File Name, using regx

查看:99
本文介绍了使用regx从文件名中提取月份和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将excel文件名格式化为特定模板



I have excel file names formatted in to a specific template

ABC_V3_Weekly_Abcdef_from_NOV_05_to_NOV_12





输出应该是这样的



FromDate:05/09/2014

ToDate:12 / 09/2014



2014年是系统年



Output should like this

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

2014 is System Year

推荐答案

尝试:

Try:
(?<frommonth>\w\w\w)(?:_)(?<fromday>\d\d)(?:_to_)(?<tomonth>\w\w\w)(?:_)(?<today>\d\d)

然后将月份组转换为数字并包含日期来自DateTime.Now



使用正则表达式完成整个事情是不切实际的 - 它是文本处理器,而不是日期处理器!使用Regex进行信息提取更加简单(并且更易于维护),然后使用您的首选语言进行处理。



错字和虚假HTML结束标签... [/ edit]

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]


这是一种方法。

This is one way to do it.
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"));
}


检查这希望这会对你有所帮助。



Check this hope this will help you.

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");


这篇关于使用regx从文件名中提取月份和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆