Swapping two parts of the namefile ONLY IF the are wrong

A swapping-ground for Regular Expression syntax

Swapping two parts of the namefile ONLY IF the are wrong

Postby Bishop » Tue Oct 13, 2020 11:10 am

Dear All,

I would like to swap two blocks of characters if their order is not correct:

c38198_600_luc is the correct order
c38198_luc_600 is incorrect

the 3 digit code varies only its first digit, I will always have either 400, 600, or 700
the 3-letter code can have many combinations of only letters and only ever 3 characters
the cxxxxx part at the beginning should always stay there, and the c should always be sentence case, it can happen sometimes that it is capital
sometimes I can have dashes - instead of underscores _, I want to make sure they are all changed to underscores
sometimes I can have some more text/digits/symbols after these three blocks, such as c38198_600_luc_mtp2, I want to keep these extra characters as they are

I could fix most of these requirements with the basic functions of the software but the swap with the IF condition got me pretty stuck, it would be great if you could help me with this :D
Bishop
 
Posts: 1
Joined: Tue Oct 13, 2020 10:50 am

Re: Swapping two parts of the namefile ONLY IF the are wrong

Postby therube » Tue Oct 13, 2020 6:43 pm

See if this covers it?

1:RegEx:
Code: Select all
Match:  (.*?)[_-]([a-zA-Z]{3})[_-](\d\d\d)(.*)
Replace:  \1_\3_\2\4


match anything up to the first _ or -
match three alpha chars, followed by _ or -
match the next set of 3 digits
rearrange those items, setting any - to _
& copy anything else that may follow

You could add additional checks in there
Likewise 4:Case can be thrown in

Code: Select all
Copy of Copy of C38198_LUC_600_mpt2 ->
   Copy of Copy of C38198_600_LUC_mpt2
Copy of c38198_luc_600_mpt2 ->
   Copy of c38198_600_luc_mpt2
c38198_luc_600 ->
   c38199_600_luc
therube
 
Posts: 927
Joined: Mon Jan 18, 2016 6:23 pm


Return to Regular Expressions


cron