Regular expression to remove a section of text

Advanced Renamer forum
#1 : 05/07-15 23:39
Sailor Guy
Sailor Guy
Posts: 23
I have files named:

19970910-MCD_Spain_Study_Abroad-001.jpg
19970910-MCD_Spain_Study_Abroad-002.jpg
19970910-MCD_Spain_Study_Abroad-003.jpg
19970910-MCD_Spain_Study_Abroad-004.jpg
19971112-MCD_Spain_Study_Abroad-001.jpg
19971112-MCD_Spain_Study_Abroad-002.jpg

I want to write a regular expression that will remove all the text between and including the first "-" hypen and the last (second) hyphen...

These are the desired results:

19970910-001.jpg
19970910-002.jpg
19970910-003.jpg
19970910-004.jpg
19971112-001.jpg
19971112-002.jpg

I thought I could do something like "(-.*-)" but it doesn't work. The hyphens seem to be causing the problem.

Can someone help me fix my problem?

Thank you,

Sailor Guy


05/07-15 23:39 - edited 09/07-15 23:45
#2 : 06/07-15 10:56
Tester123
Tester123
Posts: 92
Reply to #1:
Instead of the seemingly more obvious REMOVE method, try the REPLACE method, with

Text to be replaced: (\d{8})(.+)(-\d{3})
Replace with: \1\3
Use regular expression: Tick

Summary: Look for 8 digits, followed by at least one of any character, followed by a hyphen and 3 digits.
Replace this with the 8 digits, ignore the middle section of any characters, and include the trailing hyphen and 3 digits.

The difference is that with the remove option, it's not easy to say 'not including the last hyphen' (since AR unfortunately does not support lookahead functionality), while for the replace method, we can include only what we want.

E.g.
Remove=get rid of what we DON'T want
Replace=include what we DO want


06/07-15 10:56
#3 : 09/07-15 20:59
Sailor Guy
Sailor Guy
Posts: 23
Tester123, thank you for your prompt response.

I have a slight variation of your regular expression.

Instead of replacing specifically 8 digits at the beginning (for the first token) and specifically 3 digits at the end (for the third token), my goal is to replace any text between the first and second hyphens with a single hyphen.

I'll expand my examples to illustrate...

19970111-Some_Amount_of_Variable_Text-0001.jpg --> 19970111-0001.jpg
mcd011-More_Variable_Text-001.jpg --> mcd011-001.jpg
photoset1234-Text_I_Want_to_Eliminate-01.jpg --> photoset1234-01.jpg

What would the regexp look like for this scenario?

Thanks,

Sailor Guy



09/07-15 20:59
#4 : 09/07-15 22:03
Tester123
Tester123
Posts: 92
Reply to #3:

No problem.

"My goal is to replace any text between the first and second hyphens with a single hyphen". This is the kind of description that is helpful otherwise anyone trying to help has to try to figure out using just the example(s) provided.

This is a good tip for anyone who is stuck and wants help quickly with accurate answers that work according to how you want it, and not how the person who offers an answer thinks you want it.

Anyway, here's the answer to this one:

Text to be replaced: (.*)(-.*-)(.*)
Replace with: \1-\3
Use regular expression: Tick

Quick Summary:
Define three groups: (1) is all characters up to but not including the first hyphen (2) all characters between and including the two hyphens (3) all characters after the second hyphen. Replace with group (1), a hyphen and group (3).


09/07-15 22:03
#5 : 09/07-15 23:34
Sailor Guy
Sailor Guy
Posts: 23
Tester123,

U da man!

Thank you for the quick response! The new regexp does exactly what I need.

And interestingly, I was on the right track with my own experimentation, but as I said, it "there appears to be a problem with the hyphens."

I found out after copying-and-pasting your revised regexp into AR that even your solution wasn't working on all the files. So I renamed a few test files and found out that an entire folder of original files I was testing with looked correct, but, in fact, the "-" hyphen characters weren't actually the correct Ascii code for "-". They were some other Ascii value I haven't determined yet. That was fun figuring out.

So I had to develop another quick regexp that replaced the two bogus "-" characters with correct hyphens and Viola! everything worked. It was only one folder out of literally hundreds that had the hyphen problem (and I'm not actually sure what happened. I'm just chalking it up to "sun spots!" ;^)

Your regexp identified the problem and provided the path to the complete solution.

There ain't nothin' "regular" about your expressions, Tester123. You're a ninja / Jedi. ;^)

Thank you!

Sailor Guy


09/07-15 23:34 - edited 09/07-15 23:44
#6 : 11/07-15 13:25
Tester123
Tester123
Posts: 92
Reply to #5:
Glad I can help. Keep on trying examples and persevere and you'll soon get the hang of it. It's a bit cryptic at the start, but it's well worth it in the end.


11/07-15 13:25 - edited 11/07-15 13:27