Conditional Replace

A swapping-ground for Regular Expression syntax

Conditional Replace

Postby cempey » Mon Apr 06, 2009 7:14 pm

Hello,

I have a list of files in the format aa_bb_fname_lname_ccc.jpg. I would like to change the underscore between fname and lname to a space. aa, bb are both two charachter strings, fname, lname and ccc may be various lengths. It will always be the first underscore I want to change beyond the 7th position.

Can BRU do this, and if so, what regex to I need?

Chris
cempey
 
Posts: 2
Joined: Mon Apr 06, 2009 7:10 pm

Re: Conditional Replace

Postby GMA » Tue Apr 07, 2009 3:32 am

Hi, Chris:
You got me a little confused with the "first underscore [...] beyond the 7th position" part.
From what you say in your second sentence, I understand that you want to change the names from "aa_bb_fname_lname_ccc.jpg" to "aa_bb_fname lname_ccc.jpg" (where "aa" and "bb" always have two charachters, and "fname", "lname" and "ccc" may vary their lengths). In that case you should use the following RegEx:

MATCH: (.._.._)(.*?)_(.*)
REPLACE: \1\2 \3

Regards,

Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina

Re: Conditional Replace

Postby cempey » Tue Apr 07, 2009 5:02 pm

Thank you Gabriel,

That worked perfectly and had the additional benefit of giving me some understanding of Regex. Some playing around with this search and replace has taught me a valuable lesson that I will be able to use in the future.

Sincerely,
Chris
cempey
 
Posts: 2
Joined: Mon Apr 06, 2009 7:10 pm


Return to Regular Expressions


cron