Remove everything between two characters

A swapping-ground for Regular Expression syntax

Remove everything between two characters

Postby Xii-Nyth » Wed Feb 16, 2022 12:08 am

Hi, I'm attempting to remove all characters between the last # and an _ (including the #)

Ex:
Perfect Dark#02640111#2#0#CAEBB7CE_ciByRGBA.png
Perfect Dark#02640111#2#0_ciByRGBA.png

I got as far as using:
Code: Select all
Match (.+)#

Code: Select all
Replace \1


Which results in:
Perfect Dark#02640111#2#0.png

Unfortunately the suffix is not always ciByRGBA so I can't simply add it back afterwards.

Thank you!
Xii-Nyth
 
Posts: 4
Joined: Tue Feb 15, 2022 11:48 pm

Remove the last # and everything until the next _

Postby Luuk » Wed Feb 16, 2022 1:16 am

Just add another group to match the "_ and the ending text" ...
The .*? does say until the very next underscore", in case sometimes an underscore must also be removed.

(.+)#.*?(_.*)
\1\2
Luuk
 
Posts: 693
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove everything between two characters

Postby Xii-Nyth » Wed Feb 16, 2022 4:51 am

That works perfectly as I had requested!

I made a slight mistake and forgot that on files such as:
Code: Select all
Perfect Dark#5D2D2A6A#4#1_all.png

this would accidentally rename them to:
Code: Select all
Perfect Dark#5D2D2A6A#4_all.png


Still helpful over the original though since some files ended with ...RGB and not ...RGBA, meaning only _all was the condition it didn't work due to it not having the wildcard to be removed.
Was able to get through the rest of the ~1200 files in 10 minutes and would be instantaneous if you could sort by length of file name in the ui and just select all the longer names...

Anyways this is more than good enough since I'd imagine it would take more time to figure out what to do to avoid files that already have the wildcard removed then the total time spent by people needing to rename n64 textures to do what I did... especially since most games don't use wildcards
Xii-Nyth
 
Posts: 4
Joined: Tue Feb 15, 2022 11:48 pm

Remove last# and "everything" until _ (except 1-digit)

Postby Luuk » Wed Feb 16, 2022 8:02 am

If needing to remove #text like...

Perfect Dark#02640111#2#0#CAEBB7CE_ciByRGBA.png ======> Perfect Dark#02640111#2#0_ciByRGBA.png
Perfect Dark#5D2D2A6A#4#ABC_all.png ==================> Perfect Dark#5D2D2A6A#4_all.png
Perfect Dark#1234ABCD#4#A_all.png ====================> Perfect Dark#1234ABCD#4_all.png
Perfect Dark#1234AAA#4#11_all.png ====================> Perfect Dark#1234AAA#4_all.png
Perfect Dark#5D2D2A6A#4#1_all.png ====================> (no change, because only 1-digit) ???

There can be many different ways to conduct like this, but it does always depend on your other filenames.
Probably the easiest way is something like...
(.+)#(?!\d_)[^#]+?(_.*)
\1\2

The [^#]+? says "1-or-more characters, but none of them can be #".
The (?!\d_) is a negative-lookahead, so it tells [^#]+? to not match "only 1-digit before the underscore".
Luuk
 
Posts: 693
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove everything between two characters

Postby Xii-Nyth » Thu Feb 17, 2022 4:33 am

Perfect! Thank you so much
Xii-Nyth
 
Posts: 4
Joined: Tue Feb 15, 2022 11:48 pm


Return to Regular Expressions