Regular Expression to Remove Duplicate Numbers in Filename

A swapping-ground for Regular Expression syntax

Regular Expression to Remove Duplicate Numbers in Filename

Postby jjrogers » Mon Oct 18, 2010 11:31 am

I am not good with regular expressions, but believe it is what I need to use. I have about 5000 files named in a format similar to this:

maryborough_6594_6594_r
bundaberg_3456_3456_r

I need to remove the duplicate 6594 (or 3456) so that my filename reads

maryborough_6594_r
bundaberg_3456_r

Any help you can give would be very much appreciated.
jjrogers
 
Posts: 4
Joined: Sat Apr 01, 2006 3:36 am

Re: Regular Expression to Remove Duplicate Numbers in Filename

Postby Stefan » Mon Oct 18, 2010 7:48 pm

jjrogers wrote:I am not good with regular expressions, but believe it is what I need to use. I have about 5000 files named in a format similar to this:

maryborough_6594_6594_r
bundaberg_3456_3456_r

I need to remove the duplicate 6594 (or 3456) so that my filename reads

maryborough_6594_r
bundaberg_3456_r

Any help you can give would be very much appreciated.


Based on your provided examples, can we simple search for:
one-or-more chars, ==> match (\w+), store in group (1)
followed by an underscore, ==> match _
followed by one-or-more digits, match (\d+), store in group (2)
followed by an underscore, ==> match _
followed by one-or-more digits, ==> match (\d+), store in group (3)
followed by an underscore, ==> match _
followed by an "r" ? ==> match r

Then we replace only with group 1 and 2, dropping group 3.


FROM:
maryborough_6594_6594_r
bundaberg_3456_3456_r
TO:
maryborough_6594_r
bundaberg_3456_r

DO:
RegEx(1)
Match: (\w+)_(\d+)_(\d+)_r
Repla: \1_\2_r



.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU


Return to Regular Expressions