Rename using RegEx

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Bulgrien
    Carpal Tunnel
    • Oct 2007
    • 1772

    Rename using RegEx

    The rename dialog has an option for renaming using regular expressions, but the help file does not give any examples of a rename using regex. Please provide an example.

    In particular, I am hoping that the regex rename overcomes the limitations of a DOS-based rename. In DOS, for example, it is extremely difficult (if not impossible) to rename a group of files by adding or removing a prefix from the front of the filename:

    fileone.ext --> new_fileone.ext
    filetwo.ext --> new_filetwo.ext
    filethree.ext --> new_filethree.ext
    filefour.ext --> new_filefour.ext

    or

    new_fileone.ext --> fileone.ext
    new_filetwo.ext --> filetwo.ext
    new_filethree.ext --> filethree.ext
    new_filefour.ext --> filefour.ext

    Likewise, DOS rename cannot change the case of the filenames:

    fileone.ext --> FileOne.ext
    filetwo.ext --> FileTwo.ext
    filethree.ext --> FileThree.ext
    filefour.ext --> FileFour.ext

    I am hoping that a regex rename will evaluate each file with the regex rules to determine how to rename the file, and that both of these DOS shortcomings can be overcome... but I am not RegEx savvy and would appreciate examples to learn from and model after.
    BC v4.0.7 build 19761
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  • Chris
    Team Scooter
    • Oct 2007
    • 5538

    #2
    Michael,

    To add a prefix, use the following regular expression masks:
    Old Mask: (.*)
    New Mask: new_$1

    Placing () around an expression saves the matched text. To access the saved text, use the variables $1, $2, etc. In regular expressions this is called a "backreference".

    I don't know how you would change the character case of a filename using regular expressions.
    Chris K Scooter Software

    Comment

    • Michael Bulgrien
      Carpal Tunnel
      • Oct 2007
      • 1772

      #3
      Great! Removing the prefix was a piece of cake also:

      Old Mask: new_(.*)
      New Mask: $1

      Edit: I've since discovered that a regular expression rename does not have to represent the entire file name. To remove the prefix, all I had to do was specify the prefix in the Old Mask and leave the New Mask blank.
      Last edited by Michael Bulgrien; 02-Apr-2008, 10:46 PM.
      BC v4.0.7 build 19761
      ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

      Comment

      • Michael Bulgrien
        Carpal Tunnel
        • Oct 2007
        • 1772

        #4
        Originally posted by Chris
        I don't know how you would change the character case of a filename using regular expressions.
        According to my quick research, perl should change case with \U if you have the backreference in double quotes.

        I tried this:

        Old Mask: file(.)(.*)
        New Mask: "File\u$1$2"

        But Cirrus tells me that "Target filename is invalid".
        BC v4.0.7 build 19761
        ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

        Comment

        • Michael Bulgrien
          Carpal Tunnel
          • Oct 2007
          • 1772

          #5
          According to my research:

          \l and \u convert the next character to lower or upper case, respectively

          \L or \U indicates a lasting conversion of case, until terminated by \E or thrown over by another \U or \L

          If there is no \E , case is converted until the end of the string.

          The regexps \L\u$word or \u\L$word convert the first character of $word to uppercase and the rest of the characters to lowercase.
          BC v4.0.7 build 19761
          ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

          Comment

          • Michael Bulgrien
            Carpal Tunnel
            • Oct 2007
            • 1772

            #6
            Could I have an update on this, please. The help file says "Perl-compatible regular expressions" and the case-conversion syntax above is from the Perl web site. Can BC3 be made to correctly interpret this syntax?
            BC v4.0.7 build 19761
            ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

            Comment

            • Tim
              Team Scooter
              • Oct 2007
              • 786

              #7
              We'll look into this when we have some time, but as our help file says: "The regular expression support in Beyond Compare is a subset of the Perl Compatible Regular Expression (PCRE) syntax."
              Tim T Scooter Software

              Comment

              Working...