Regrouping a file name with Regexp?

A swapping-ground for Regular Expression syntax

Regrouping a file name with Regexp?

Postby smorris » Mon Jul 19, 2010 9:32 pm

I'm a total regexp newb, but I was wondering if anyone here could help me out. I'm trying to use Bulk Rename Utility to rename a batch of scanned medical records to a new scheme. Basically, the current files look like this:
Code: Select all
IME John Smith 01-01-10.tiff
UR Jane Doe 01-03-05.tiff
IME John Doe 04-04-05.tiff
and I want them to look like this:
Code: Select all
20100101 John Smith IME.tiff
20050301 Jane Doe UR.tiff
20050404 John Doe IME.tiff


The first part, the type of file, can be IME, UR, ADD UR, ADD IME, 2ND IME, or 2ND UR. The name is usually first and last, but some files have up to five names listed. The date is currently in mm-dd-yy format and I want it to be converted to yyyymmdd.

How would this be done with regexps and the Batch Renaming Utility software? Any input would be appreciated greatly! I have thousands of these files that I would like to rename and doing them by hand would take weeks. Thanks!
smorris
 
Posts: 2
Joined: Mon Jul 19, 2010 9:17 pm

Re: Regrouping a file name with Regexp?

Postby Stefan » Tue Jul 20, 2010 8:01 am

Hi smorris, welcome.

smorris wrote:I'm a total regexp newb, but I was wondering if anyone here could help me out. I'm trying to use Bulk Rename Utility to rename a batch of scanned medical records to a new scheme. Basically, the current files look like this:
Code: Select all
IME John Smith 01-01-10.tiff
UR Jane Doe 01-03-05.tiff
IME John Doe 04-04-05.tiff
and I want them to look like this:
Code: Select all
20100101 John Smith IME.tiff
20050301 Jane Doe UR.tiff
20050404 John Doe IME.tiff


The first part, the type of file, can be IME, UR, ADD UR, ADD IME, 2ND IME, or 2ND UR. The name is usually first and last, but some files have up to five names listed. The date is currently in mm-dd-yy format and I want it to be converted to yyyymmdd.

How would this be done with regexps and the Batch Renaming Utility software? Any input would be appreciated greatly! I have thousands of these files that I would like to rename and doing them by hand would take weeks. Thanks!


FROM:
UR Jane Doe 01-03-05.tiff
TO:
20050301 Jane Doe UR.tiff

General request:
match some signs till an space =========> "UR " ========> use RegEx: "(.+?)\s" ====> Store in replace group 1 ===> "\1"
match some signs till an space ========> "Jane Doe " =============> "(.+)\s" ====> Replace group 2 ==========> "\2"
following by two digits and an hyphen ===> "01-" ==================> "(\d\d)-" ===> Replace group 3 ==========> "\3"
following by two digits and an hyphen ===> "03-" ==================> "(\d\d)-" ===> Replace group 4 ==========> "\4"
following by two digits ===============> "05" ===================> "(\d\d)" ===> Replace group 5 ==========> "\5"

Replace with additional "20", then them what is match in group 5 => "\5", then group 4 => "\4", then "\3", space " ", "\2", space " " and "\1"


So try
RegEx(1)
Search: (.+?)\s(.+)\s(\d\d)-(\d\d)-(\d\d)
Repla: 20\5\4\3 \2 \1




General notes/ Disclaimer

Hope this helps ? :D
If yes, please help two others too. And consider an donation to the tools autor.


Please note:

Always don't trust me! Test with test files first!
* Usually i do a few tests on this issue only!
* So please test my solution with some test files first before you destroy your data.
* Select one or more files in the Name column to watch how the New Name will be.

RegEx is an pattern matching solution, so all your files have to fit into the same pattern.
If they not, you have to separate them and run some more actions against them.

To find your own solution you have to virtual (in mind) split your file names/strings into parts
following the rules of the regular expression syntax, see the help file coming with your application.
(Please note that there are several flavors of RE engines and also different implementations into apps
and even different ways of doing or thinking, so your expiriences may differ from my explanation)
Once you have split your string into parts you can decide which to use into replacement by grouping the pattern
into (...) parenthesis to which you can refer by using "\1" or "$1" signs later, or which to drop and which to modify.

My solution depends on the quality of your examples.
* It's always an good idea to provide all possibilities of file name pattern in question.
* That would give the supporter an change to do it right ;)
* If your real file names doesn't fit into your example pattern my solution may fail.

* Don't use this ' ' or " " -quotes from my explanation. They are only for clarification.
* '?' means non-greedy: stop at first match, instead of last possible.
* This (...) parenthesis are used to "group" what is found by this RegEx inside the ( )'s
to reuse this capture later as replacement by using \1 .
* Instead of ~ -signs, if used in my explanations, type an space/blank.


More Help
* online tester:
- http://rereplace.com/
- http://www.regextester.com/
- http://www.regexlib.com/RETester.aspx

* online help:
- www.regular-expressions.info
- www.regexlib.com/
- www.regexlib.com/CheatSheet.aspx

See this both oldest threads in the "Regular Expressions" forum for an RegEx syntax overview:
=> Getting Started: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=5
=> Go ahead: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=27
There you will find more examples and tips as you may find in other threads in the "Regular Expressions" sub-forum.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Regrouping a file name with Regexp?

Postby smorris » Tue Jul 20, 2010 6:40 pm

Thanks a bunch! The Regexp worked perfectly :D
smorris
 
Posts: 2
Joined: Mon Jul 19, 2010 9:17 pm


Return to Regular Expressions