views:

155

answers:

5

Hello,

In the application I am currently working on, I have an option to create automatic backups of a certain file on the hard disk. What I would like to do is offer the user the possibility to configure the name of the file and its extension.

For example, the backup filename could be something like : "backup_month_year_username.bak". I had the idea to save the format in the form of a regular expression. For the example above, the regexp would look like :

"^backup_(?<Month>\d{2})_(?<Year>\d{2})_(?<Username>\w).(?<extension>bak)$"

I thought about using regex because I will also have to browse through the directory of backuped files to delete those older than a certain date. The main trouble I have now is how to create a filename using the regex. In a way I should replace the tags with the information. I could do that using regex.replace and another regex, but I feel it's a big weird doing that and it might be a better way.

Thanks

[Edit] Maybe I wasn't really clear in the first go, but the idea is of course that the user (in this case an admin that will know regex syntax) will have the possibility to modify the form of the filename, that's all the idea behind it[/Edit]

A: 

If the filename is always in this form, there is no reason for a regex, as it's easier to process with string.Split ...

Noon Silk
+1  A: 

... and if the regex changes, it is next to impossible to reconstruct a string from a given regex.


Edit:

Create some predefined "place-holders": %u could be the user's name, %y could be the year, etc.:

backup_%m_%y_%u.bak

and then simple replace the %? with their actual values.

Bart Kiers
So... any idea on how to solve the problem?
Gimly
@Gimly: see the edit.
Bart Kiers
Well, yes that's something I could do, but then it will be really difficult to get back the information from the generated filename.
Gimly
@Gimly: so, you want to generate a valid string from a certain regex AND the other way around: extract the information from a certain file name with the aid of that same regex?
Bart Kiers
Yes, that's exactly what I'd like to do.
Gimly
But there is still too much info missing IMO. After reading a file name, how are you supposed to know what the admin choose at that particular time (I assume the admin can change the format, right)? I mean take the file name 'backup_12.bak', 12 could be the month or the day. Without additional information, this cannot be reliably reversed.
Bart Kiers
Yes, exactly, that's why I was using the groups in the regex, to specify that something is a Month, a Year, a Username or whatever. I've tried my solution on a sample thing and it looks like it's working. It might not be really elegant, but at least it's working. What do you think?
Gimly
A: 

With Bart's solution it is easy enough to split (using string.Split) the generated file name using underscore as the delimiter, to get back the information.

ShellShock
Yes... but no, because the user might choose to not delimit its tags in this way. If he says its filename is :"backup%m%y-%u" then I can't use string.split to get the information back.
Gimly
+1  A: 

It sounds like you're trying to use the regular expression to create the file name from a pattern which the user should be able to specify.

Regular expressions can - AFAIK - not be used to create output, but only to validate input, so you'd have the user specify two things:

  1. a file name production pattern like Bart suggested
  2. a validation pattern in form of a regular expression that helps you split the file names into their parts

EDIT
By the way, your sample regex contains an error: The "." is use for "any character", also \w only matches one word character, so I guess you meant to write

"^backup_(?<Month>\d{2})_(?<Year>\d{2})_(?<Username>\w+)\.(?<extension>bak)$"

Thorsten Dittmar
So, you think there is really no other way around? That's exactly what I wanted to avoid. Having two different patterns could lead to errors where the two strings are different.
Gimly
Well, you could have the user specify a format pattern like Bart suggested and then analyze the same pattern yourself instead of using a regex.
Thorsten Dittmar
A: 

Ok, I think I have found a way to use only the regex. As I am using groups to get the information, I will use another regular expression to match the regular expression and replace the groups with the value:

Regex rgx = new Regex("\(\?\<Month\>.+?\)");
rgx.Replace("^backup_(?<Month>\d{2})_(?<Year>\d{2})_(?<Username>\w+)\.(?<extension>bak)$"
, DateTime.Now.Month.ToString());

Ok, it's really a hack, but at least it works and I have only one pattern defined by the user. It might not work if the regex is too complex, but I think I can deal with that problem.

What do you think?

Gimly
It won't work. Consider `"^(?<Username>\w+)(?<Year>\d{2})(?<Month>\d{2})\.(?<extension>bak)$"`
Jeremy Stein