To split, or to regex


T

Tony

I've got a deal where I've wrote an app which will secure ftp reports
out to various clients. I set it up to pull key pieces of information
like ftp site etc from an xml file that has a standard layout and
accompanies each report that's run.

I have a tag for filename, but most filenames will not be static. They
will contain a date in some format that the attachment is reporting
on. Of course, various clients request filenames to be in various
patterns. So while I can create a generic ftp app, the filename
handling will need some work.

What I was thinking was letting the reports progs put the filename in
the xml file within the proper tags, with the various ways the date
can be entered within the filename and use a char that I've never seen
used in a filename as a delimiter.

I chose the caret (^) as the symbol.

I've got everything set up and am pulling the filename in as a string,
And know how to replace with a real date value but I don't know how
best to take the pieces out of the filename to change to date.

I


replace ^mm^ with mm
replace ^dd^ with yy
replace ^yyyy^ with yyyy
replace ^mmddyy^ with mmddyy
replace ^mmddyyyy^ with mmddyyyy
replace ^-dd^ with dd-1
replace ^mm-ddyy^ with mmddyy-1
replace ^mm-ddyyyy^ with mmddyyyy-1


So today, for example
filename_^mm^-^dd^-^yy^_something.zip
would be handled by the app as if
filename_09-27-06_something.zip
was passed to it from the xml file.

or
filename_^mm^_^yyyy^_something.zip
would be handled by the app as if
filename_09_2006_something.zip
was passed to it from the xml file.

and also

filename_^mm^-^-dd^-^yyyy^_something.zip
would be handled by the app as if
filename_09-26-2006_something.zip
was passed to it from the xml file.
(since the -dd would signify yesterday's day.)


What I got so far (for testing purps) for a start,,,

string tony =
"filename_^mm^-^dd^-^yy^_Something.zip";
string[] parsedData = tony.Split('^');
text1.AppendText(parsedData[1] + "\r\n");
text1.AppendText(parsedData[2] + "\r\n");
text1.AppendText(parsedData[3] + "\r\n");
text1.AppendText(parsedData[4] + "\r\n");
text1.AppendText(parsedData[5] + "\r\n");
text1.AppendText(parsedData[6] + "\r\n");

which returns
mm
-
dd
-
yy
_Something.zip


But I'm thinking Regex would be a better way to handle this and easier
to reconstruct the correct filename from.

But regex is something I don't know at all, least of all how to
auto-replace values between carats with other values
expecially when the year could be yy or yyyy

Plus, The carat is a special symbol in regex, would this strike it as
a possible char ro use my redlight?

Can one of you gurus help me finish this project??

Thanks in advance! (and sorry for the very long post.. wanted to be
sure I gave enough information)


Tony!
 
Ad

Advertisements

S

Samuel R. Neff

With Regex you could run a single search and match all the target
possible tokens then loop through all the matches, appending skipped
text from the original and replacing tokens as you go.

Here's an example.

HTH,

Sam



public class RegexDates
{
private static Regex _regex = new Regex(@"\^(mm|dd)\^");

public static void Test()
{
Test("nothing");
Test("Month_^mm^.dat");
Test("Date_^dd^.dat");
Test("^mm^_^dd^.dat");
Test("^^mm^^");
}

public static void Test(string text)
{
string replaced = ReplaceKeys(text);
Console.WriteLine("{0,-30} | {1,-30}", text, replaced);
}

public static string ReplaceKeys(string text)
{
StringBuilder outText = new StringBuilder(text.Length);

Match m = _regex.Match(text);
int lastEndIndex = 0;
while(m.Success)
{
if (m.Index > lastEndIndex)
{
outText.Append(text.Substring(lastEndIndex, m.Index -
lastEndIndex));
}

switch(m.Groups[1].Value)
{
case "mm":
outText.Append(DateTime.Now.Month.ToString());
break;

case "dd":
outText.Append(DateTime.Now.Day.ToString());
break;

default:
outText.Append(m.Value);
break;
}

lastEndIndex = m.Index + m.Length;
m = m.NextMatch();
}

if (lastEndIndex < text.Length)
{
outText.Append(text.Substring(lastEndIndex));
}

return outText.ToString();
}
}

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



I've got a deal where I've wrote an app which will secure ftp reports
out to various clients. I set it up to pull key pieces of information
like ftp site etc from an xml file that has a standard layout and
accompanies each report that's run.

I have a tag for filename, but most filenames will not be static. They
will contain a date in some format that the attachment is reporting
on. Of course, various clients request filenames to be in various
patterns. So while I can create a generic ftp app, the filename
handling will need some work.

What I was thinking was letting the reports progs put the filename in
the xml file within the proper tags, with the various ways the date
can be entered within the filename and use a char that I've never seen
used in a filename as a delimiter.

I chose the caret (^) as the symbol.

I've got everything set up and am pulling the filename in as a string,
And know how to replace with a real date value but I don't know how
best to take the pieces out of the filename to change to date.

I


replace ^mm^ with mm
replace ^dd^ with yy
replace ^yyyy^ with yyyy
replace ^mmddyy^ with mmddyy
replace ^mmddyyyy^ with mmddyyyy
replace ^-dd^ with dd-1
replace ^mm-ddyy^ with mmddyy-1
replace ^mm-ddyyyy^ with mmddyyyy-1


So today, for example
filename_^mm^-^dd^-^yy^_something.zip
would be handled by the app as if
filename_09-27-06_something.zip
was passed to it from the xml file.

or
filename_^mm^_^yyyy^_something.zip
would be handled by the app as if
filename_09_2006_something.zip
was passed to it from the xml file.

and also

filename_^mm^-^-dd^-^yyyy^_something.zip
would be handled by the app as if
filename_09-26-2006_something.zip
was passed to it from the xml file.
(since the -dd would signify yesterday's day.)


What I got so far (for testing purps) for a start,,,

string tony =
"filename_^mm^-^dd^-^yy^_Something.zip";
string[] parsedData = tony.Split('^');
text1.AppendText(parsedData[1] + "\r\n");
text1.AppendText(parsedData[2] + "\r\n");
text1.AppendText(parsedData[3] + "\r\n");
text1.AppendText(parsedData[4] + "\r\n");
text1.AppendText(parsedData[5] + "\r\n");
text1.AppendText(parsedData[6] + "\r\n");

which returns
mm
-
dd
-
yy
_Something.zip


But I'm thinking Regex would be a better way to handle this and easier
to reconstruct the correct filename from.

But regex is something I don't know at all, least of all how to
auto-replace values between carats with other values
expecially when the year could be yy or yyyy

Plus, The carat is a special symbol in regex, would this strike it as
a possible char ro use my redlight?

Can one of you gurus help me finish this project??

Thanks in advance! (and sorry for the very long post.. wanted to be
sure I gave enough information)


Tony!
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Another way could be to use a format the the DateTime.ToString(string)
method can use. For example:

'filename_'MM-dd-yy'_something.zip'

'filename_'MM_yyyy'_something.zip'
 
T

Tony

Another way could be to use a format the the DateTime.ToString(string)
method can use. For example:

'filename_'MM-dd-yy'_something.zip'

'filename_'MM_yyyy'_something.zip'

I ended up kind of cheating... Decided to have all the report apps
create a file that would contain only the name of the report to send.
I then grab the name from the file.

With the possible variants of how the date could be in the filename
and adding the possibility of also having a timestamp in the
filename,. it was the least headache way to go for all :)

I do thank the folks that replied though!!

Tony!
 
Ad

Advertisements

T

Tony

Thanks for the time and reply :)

I'll have to look at and play with the below even if I send up doing
things another way, it still looks like good info to have.. (I've been
wanting to get more regex experience anyway)

Thanks :)

Tony!


With Regex you could run a single search and match all the target
possible tokens then loop through all the matches, appending skipped
text from the original and replacing tokens as you go.

Here's an example.

HTH,

Sam



public class RegexDates
{
private static Regex _regex = new Regex(@"\^(mm|dd)\^");

public static void Test()
{
Test("nothing");
Test("Month_^mm^.dat");
Test("Date_^dd^.dat");
Test("^mm^_^dd^.dat");
Test("^^mm^^");
}

public static void Test(string text)
{
string replaced = ReplaceKeys(text);
Console.WriteLine("{0,-30} | {1,-30}", text, replaced);
}

public static string ReplaceKeys(string text)
{
StringBuilder outText = new StringBuilder(text.Length);

Match m = _regex.Match(text);
int lastEndIndex = 0;
while(m.Success)
{
if (m.Index > lastEndIndex)
{
outText.Append(text.Substring(lastEndIndex, m.Index -
lastEndIndex));
}

switch(m.Groups[1].Value)
{
case "mm":
outText.Append(DateTime.Now.Month.ToString());
break;

case "dd":
outText.Append(DateTime.Now.Day.ToString());
break;

default:
outText.Append(m.Value);
break;
}

lastEndIndex = m.Index + m.Length;
m = m.NextMatch();
}

if (lastEndIndex < text.Length)
{
outText.Append(text.Substring(lastEndIndex));
}

return outText.ToString();
}
}

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



I've got a deal where I've wrote an app which will secure ftp reports
out to various clients. I set it up to pull key pieces of information
like ftp site etc from an xml file that has a standard layout and
accompanies each report that's run.

I have a tag for filename, but most filenames will not be static. They
will contain a date in some format that the attachment is reporting
on. Of course, various clients request filenames to be in various
patterns. So while I can create a generic ftp app, the filename
handling will need some work.

What I was thinking was letting the reports progs put the filename in
the xml file within the proper tags, with the various ways the date
can be entered within the filename and use a char that I've never seen
used in a filename as a delimiter.

I chose the caret (^) as the symbol.

I've got everything set up and am pulling the filename in as a string,
And know how to replace with a real date value but I don't know how
best to take the pieces out of the filename to change to date.

I


replace ^mm^ with mm
replace ^dd^ with yy
replace ^yyyy^ with yyyy
replace ^mmddyy^ with mmddyy
replace ^mmddyyyy^ with mmddyyyy
replace ^-dd^ with dd-1
replace ^mm-ddyy^ with mmddyy-1
replace ^mm-ddyyyy^ with mmddyyyy-1


So today, for example
filename_^mm^-^dd^-^yy^_something.zip
would be handled by the app as if
filename_09-27-06_something.zip
was passed to it from the xml file.

or
filename_^mm^_^yyyy^_something.zip
would be handled by the app as if
filename_09_2006_something.zip
was passed to it from the xml file.

and also

filename_^mm^-^-dd^-^yyyy^_something.zip
would be handled by the app as if
filename_09-26-2006_something.zip
was passed to it from the xml file.
(since the -dd would signify yesterday's day.)


What I got so far (for testing purps) for a start,,,

string tony =
"filename_^mm^-^dd^-^yy^_Something.zip";
string[] parsedData = tony.Split('^');
text1.AppendText(parsedData[1] + "\r\n");
text1.AppendText(parsedData[2] + "\r\n");
text1.AppendText(parsedData[3] + "\r\n");
text1.AppendText(parsedData[4] + "\r\n");
text1.AppendText(parsedData[5] + "\r\n");
text1.AppendText(parsedData[6] + "\r\n");

which returns
mm
-
dd
-
yy
_Something.zip


But I'm thinking Regex would be a better way to handle this and easier
to reconstruct the correct filename from.

But regex is something I don't know at all, least of all how to
auto-replace values between carats with other values
expecially when the year could be yy or yyyy

Plus, The carat is a special symbol in regex, would this strike it as
a possible char ro use my redlight?

Can one of you gurus help me finish this project??

Thanks in advance! (and sorry for the very long post.. wanted to be
sure I gave enough information)


Tony!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

File Save As macro for users with different paths 6
best design for parse 29
Handling dates in VB.NET 2
Format Date (Input Masks...) 1
regex syntax 12
excel input date format 1
Help with "end if" 3
Regex - Date 5

Top