Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extracting regex from string

Extracting regex from string

Scheduled Pinned Locked Moved C#
regexgraphicstutorialquestionannouncement
11 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O ormonds

    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?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    ormonds wrote:

    Or their system may be something completely different.

    Which makes it somewhat difficult to create a regex for every possible situation.

    1 Reply Last reply
    0
    • O ormonds

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      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.

      1 Reply Last reply
      0
      • O ormonds

        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?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        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

        1 Reply Last reply
        0
        • O ormonds

          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?

          R Offline
          R Offline
          realJSOP
          wrote on last edited by
          #5

          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

          J O 2 Replies Last reply
          0
          • R realJSOP

            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

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #6

            Excepting of course there will be character restrictions.

            R 1 Reply Last reply
            0
            • O ormonds

              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?

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #7

              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.

              1 Reply Last reply
              0
              • J jschell

                Excepting of course there will be character restrictions.

                R Offline
                R Offline
                realJSOP
                wrote on last edited by
                #8

                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

                J 1 Reply Last reply
                0
                • R realJSOP

                  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

                  O Offline
                  O Offline
                  ormonds
                  wrote on last edited by
                  #9

                  Thank 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.

                  1 Reply Last reply
                  0
                  • O ormonds

                    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?

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #10

                    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/letter

                    which 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

                    1 Reply Last reply
                    0
                    • R realJSOP

                      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

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #11

                      lol...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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups