Pick up relevant information from a string using regular expression C#3.0
Posted
by Newbie
on Stack Overflow
See other posts from Stack Overflow
or by Newbie
Published on 2010-06-15T08:08:43Z
Indexed on
2010/06/15
8:12 UTC
Read the original article
Hit count: 190
c#3.0
Hi, I have a situation.
I have been given some file name which can be like
<filename>YYYYMMDD<fileextension>
some valid file names that will satisfy the above pattern are as under
xxx20100326.xls,
xxx2v20100326.csv,
x_20100326.xls,
xy2z_abc_20100326_xyz.csv,
abc.xyz.20100326.doc,
ab2.v.20100326.doc,
abc.v.20100326_xyz.xls
In what ever be the above defined case, I need to pick up the dates only. So for all the cases, the output will be 20100326.
I am trying to achieve the same but no luck.
Here is what I have done so far
string testdata = "x2v20100326.csv";
string strYYYY = @"\d{4}";
string strMM = @"(1[0-2]|0[1-9])";
string strDD = @"(3[0-1]|[1-2][0-9]|0[1-9])";
string regExPattern = @"\A" + strYYYY + strMM + strDD + @"\Z";
Regex regex = new Regex(regExPattern);
Match match = regex.Match(testdata);
if (match.Success)
{
string result = match.Groups[0].Value;
}
I am using c#3.0 and dotnet framework 3.5
Please help. It is very urgent
Thanks in advance.
© Stack Overflow or respective owner