Swithching P's With S's and S's with P's.

A swapping-ground for Regular Expression syntax

Swithching P's With S's and S's with P's.

Postby trevzilla » Fri Jun 02, 2017 12:42 am

Hi all! New to the forums. Seems like a cool place!

I use BRU pretty extensively, but have yet to run into a scenario where I've had to use the RegEx box. Well, I assume that scenario has finally hit, but I'm a little lost with it. Curious if you could offer help!

I have thousands of files that need renaming, with the general form:

005 1P (526)
007 2S (122)

Or something similar. The numbers in the parenthesis all change, as do the first numbers... so using variables I could write it this way...

xxx xP (xxx)
xxx xS (xxx)

with an 'x' being any random digit between 0-9.
Essentially I want to be able to find all the files that have P's in them and change those to S's. And Vice Versa. Change the S's to P's...

Any idea how to do that quickly and easily? I have a way that would work, but it would be neither quick nor easy. I'm assuming this can be done with one command...

Thanks!
trevzilla
 
Posts: 5
Joined: Fri Jun 02, 2017 12:31 am

Re: Swithching P's With S's and S's with P's.

Postby KenP » Fri Jun 02, 2017 1:34 am

You may be able to do it in one hit with Javascript I'm not sure, with regex it will take 2 hits.

To change the P to S
RegEx (1)
Match: (.* \d)P(.*)
Replace: \1S\2

To change the S to P
RegEx (1)
Match: (.* \d)S(.*)
Replace: \1P\2
KenP
 
Posts: 199
Joined: Sat Jul 30, 2016 11:25 am

Re: Swithching P's With S's and S's with P's.

Postby Admin » Fri Jun 02, 2017 2:07 am

Hi, yes, JavaScript supports Regular Expressions too, so the above can be combined in one script and then applied to files in one pass. However no need to use JavaScript if one pass is not required. cheers
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Swithching P's With S's and S's with P's.

Postby trevzilla » Fri Jun 02, 2017 2:09 am

What about with a Character Translation? That sounds promising, but I couldn't quite make it work either...
trevzilla
 
Posts: 5
Joined: Fri Jun 02, 2017 12:31 am

Re: Swithching P's With S's and S's with P's.

Postby trevzilla » Fri Jun 02, 2017 2:17 am

Ah, and I failed...I guess I have directories too that need the same to be done...P's to S's and Vice Versa.

I was actually thinking three passes could work, but that was my "not easy" solution...

Essentially name the P's to a temporary letter, then name the S's to P's (as to not accidentally name any directories the same thing) then name the temporary letter over to S's. I was going to do this with Replace (3), and just replace P with Z, then S with P, then Z with S.

I guess I was just hoping to do one pass though.

Thoughts?
trevzilla
 
Posts: 5
Joined: Fri Jun 02, 2017 12:31 am

Re: Swithching P's With S's and S's with P's.

Postby KenP » Fri Jun 02, 2017 11:30 am

Thinking about it more I think you will need to start by changing one of the letters to something other than P or S anyway, so it may need 3 passes.

The problem is of course that once you've changed say the Ps to Ss, when you then change the Ss to Ps the original Ps that have now become S will be changed back to their original P.

The only way I can see to do it in one hit would be to use loops in JavaScript, though I'm not sure if BRU JavaScript supports loops.

Having said that, BRU is quite fast so it's not going to take long even doing it in 3 passes.

Change the P to Z
Change the S to P
Change the Z to S

Even with thousands of files it's not going to take that long unless you're working on a really old computer.
KenP
 
Posts: 199
Joined: Sat Jul 30, 2016 11:25 am

Re: Swithching P's With S's and S's with P's.

Postby Admin » Fri Jun 02, 2017 12:57 pm

If you have a commercial license for BRU and you can use the JavaScript functionality of BRU, I can post the script to do this, cheers
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Swithching P's With S's and S's with P's.

Postby BRUsky » Sat Jun 10, 2017 12:15 am

trevzilla,

Another way would be,

1. change " (" to "A("
2. change "PA" to "S "
3. change "SA" to "P "

It's still 3 passes but could inhibit possible confusion/accidents
arising from having to completely change a letter.
BRUsky
 
Posts: 6
Joined: Tue Jun 06, 2017 9:47 pm


Return to Regular Expressions


cron