Is there a way to transform string like
http://blar1.s3.shamazonaws.com/ZZ/bstd-20140801-004000-0500.time.gz
to
C:\Temp\2014\08\
using single regular expression?
There a lot of files to download regulary, and I need to store these files in a directorty structure organized by year and month. They all have same date part in name - like "20140801-004000-0500" in my example here, but other parts of link can differ.
You can use this regex:
^ # start of the string
.+- # match everything until the first hyphen
(?<year>\d{4}) # capture the first four digits into a group named year
(?<month>\d{2}) # capture the next two digits into a group named month
(?<day>\d{2}) # you get the idea...
-.+$ # match everything else until the end of the string
The following snipped should do the work:
string strRegex = @"^.+-(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})-.+$";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"http://blar1.s3.shamazonaws.com/ZZ/bstd-20140801-004000-0500.time.gz";
string strReplace = @"C:\Temp\${year}\${month}";
return myRegex.Replace(strTargetString, strReplace);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.