Extracting regex from string
-
I am writing an app in which the user will - once only - input their file naming system for certain types of files. For example, they might save all project CAD models in the format "Project12345-Name-Drawing-A" where:- Project is the same for all projects, i.e. a literal 12345 is the project number (variable length) - is required Name is the project name (variable length) - is required Drawing is the drawing name - is required A - might be the version and is required to be an upper case alphabetic character. Or their system may be something completely different. Once set up the software can only save files which comply with the standard naming format. Sounds like a job for regex. It also sounds like someone might have done it before - extracted the regex pattern from a given string. (I'm assuming that the initial setup would require each character to be tagged in some way) Any suggestions?
-
I am writing an app in which the user will - once only - input their file naming system for certain types of files. For example, they might save all project CAD models in the format "Project12345-Name-Drawing-A" where:- Project is the same for all projects, i.e. a literal 12345 is the project number (variable length) - is required Name is the project name (variable length) - is required Drawing is the drawing name - is required A - might be the version and is required to be an upper case alphabetic character. Or their system may be something completely different. Once set up the software can only save files which comply with the standard naming format. Sounds like a job for regex. It also sounds like someone might have done it before - extracted the regex pattern from a given string. (I'm assuming that the initial setup would require each character to be tagged in some way) Any suggestions?
ormonds wrote:
Any suggestions?
Most modern games let you save the file without having to worry about filename-restrictions. They do this by asking for a string from the user to identify the file (a caption), and generating a save-file name that gets associated with that caption. Some will even simulate folders or categories :) Gives the advantage that the user no longer directly interacts with the filesystem, is no longer responsible for giving a "valid" filename (but any string he/she chooses), and gives the enduser a bit more flexibility.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
I am writing an app in which the user will - once only - input their file naming system for certain types of files. For example, they might save all project CAD models in the format "Project12345-Name-Drawing-A" where:- Project is the same for all projects, i.e. a literal 12345 is the project number (variable length) - is required Name is the project name (variable length) - is required Drawing is the drawing name - is required A - might be the version and is required to be an upper case alphabetic character. Or their system may be something completely different. Once set up the software can only save files which comply with the standard naming format. Sounds like a job for regex. It also sounds like someone might have done it before - extracted the regex pattern from a given string. (I'm assuming that the initial setup would require each character to be tagged in some way) Any suggestions?
You're better off with a "dictionary" that indexes all the (generated) files names with properties worth tracking; instead of trying to force a "file name standard" which always seems to fall short; or drives users mad.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
I am writing an app in which the user will - once only - input their file naming system for certain types of files. For example, they might save all project CAD models in the format "Project12345-Name-Drawing-A" where:- Project is the same for all projects, i.e. a literal 12345 is the project number (variable length) - is required Name is the project name (variable length) - is required Drawing is the drawing name - is required A - might be the version and is required to be an upper case alphabetic character. Or their system may be something completely different. Once set up the software can only save files which comply with the standard naming format. Sounds like a job for regex. It also sounds like someone might have done it before - extracted the regex pattern from a given string. (I'm assuming that the initial setup would require each character to be tagged in some way) Any suggestions?
The only name restriction you should have (if any) is the file extension. To enforce this, simply append your extension to whatever the user enters for a filename. After that, who really cares what the filename is, as long as it's got the correct extension for your app.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
The only name restriction you should have (if any) is the file extension. To enforce this, simply append your extension to whatever the user enters for a filename. After that, who really cares what the filename is, as long as it's got the correct extension for your app.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
I am writing an app in which the user will - once only - input their file naming system for certain types of files. For example, they might save all project CAD models in the format "Project12345-Name-Drawing-A" where:- Project is the same for all projects, i.e. a literal 12345 is the project number (variable length) - is required Name is the project name (variable length) - is required Drawing is the drawing name - is required A - might be the version and is required to be an upper case alphabetic character. Or their system may be something completely different. Once set up the software can only save files which comply with the standard naming format. Sounds like a job for regex. It also sounds like someone might have done it before - extracted the regex pattern from a given string. (I'm assuming that the initial setup would require each character to be tagged in some way) Any suggestions?
ormonds wrote:
It also sounds like someone might have done it before - extracted the regex pattern from a given string. ... Any suggestions?
1. Don't do that. 2. Allow, but do not require them to enter a regex. The problem with this is that either you must carefully validate that (limit their regex) or you run the risk of allowing them to entire something that could be valid but could have serious problems (read the book "Mastering Regular Expressions".) 3. They can submit a regex to the operations team. They validate it and if ok enter it in for the account via a Operations only option.
-
FFS, I assumed that wouldn't even have to be said.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
The only name restriction you should have (if any) is the file extension. To enforce this, simply append your extension to whatever the user enters for a filename. After that, who really cares what the filename is, as long as it's got the correct extension for your app.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013Thank you. The app is an extension of one written for my own company where we have rules around how BIM files can be named. It seemed to me that the app might be useful for others. You are quite right, there needn't be any rules, just a common location for saving such files.
-
I am writing an app in which the user will - once only - input their file naming system for certain types of files. For example, they might save all project CAD models in the format "Project12345-Name-Drawing-A" where:- Project is the same for all projects, i.e. a literal 12345 is the project number (variable length) - is required Name is the project name (variable length) - is required Drawing is the drawing name - is required A - might be the version and is required to be an upper case alphabetic character. Or their system may be something completely different. Once set up the software can only save files which comply with the standard naming format. Sounds like a job for regex. It also sounds like someone might have done it before - extracted the regex pattern from a given string. (I'm assuming that the initial setup would require each character to be tagged in some way) Any suggestions?
Hi, I would not use Regex at all. Instead, have the filename pattern specified as a string where some special codes are allowed, and will be replaced by their actual value. Your example could be defined by
"Project{P}-{N}-{D}-{V}.dat"
, and now your code should replace{P} by the project name
{N} by the project number
{D} by the drawing number
{V} by the version number/letterwhich basically takes four calls to
string.Replace()
, where you:1. may or may not enforce these codes to be present;
2. may or may not allow for case-insensitivy when looking for those codes;
3. must take care of avoiding illegal filenames; you may have to create a project name clone
that does not contain any character that would be unacceptable in a file name.:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
FFS, I assumed that wouldn't even have to be said.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013lol...not in my experience. Once they start messing with regexes all normal expectations go out the window. They tend to allow things they shouldn't or fail to disallow others. Even presuming they get the regex right the first time. I have seen multiple cases where people didn't even understand what a character class in a regex was. As one ready example just do a general search for people looking to make a 'easy' regex to do email validation.