remove last half of a string

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sbrady19
Posts: 337
Joined: 12 Apr 2018, 05:22
Location: Central Florida
Contact:

remove last half of a string

Post by sbrady19 » 17 Aug 2022, 19:15

I loop a folder and get the file I'm looking for:
UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif

I need to remove everthing after and including "_instrumental_"
cant figure out how to use SubST() or regex........little help please. Thanks.

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: remove last have of a string

Post by sofista » 17 Aug 2022, 19:57

Try:

Code: Select all

str := "UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif"
MsgBox, % RegExReplace(str, "^(.*)_Instrumental_.*", "$1")
; output: UPM_Tru154_2_Beautiful_Moment

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: remove last have of a string

Post by flyingDman » 17 Aug 2022, 20:43

You choose:

Code: Select all

str := "UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif"
MsgBox, % substr(str, 1, instr(str,"_Instrumental") - 1)
; output: UPM_Tru154_2_Beautiful_Moment
14.3 & 1.3.7

sbrady19
Posts: 337
Joined: 12 Apr 2018, 05:22
Location: Central Florida
Contact:

Re: remove last half of a string

Post by sbrady19 » 18 Aug 2022, 07:46

Sofista, thank you so much. This really helped. I do audio for television and I need to get song names used in our projects. I used to click on a clip and copy the filename. Long and laborious. I knew there was a way to loop a folder and get every file whose name contains MAIN TRACK, or ALTERNATIVE, or BACKING VOCALS, this does exactly what I need and strips out the unused stuff, gives me just the song name and CD number. Thank you so much.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: remove last half of a string

Post by BoBo » 18 Aug 2022, 08:39

Code: Select all

str:="UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif"
MsgBox % StrSplit(str, "_Instrumental").1
:mrgreen:

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: remove last half of a string

Post by sofista » 18 Aug 2022, 13:38

@sbrady19 You are welcome. Taking a second look at this, there is no need for a destructive solution, such as RegExReplace, because a simple match is enough. For example:

Code: Select all

str := "UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif"
MsgBox, % (m1, RegExMatch(str, "^(.*)_Instrumental", m))
There is no big difference between both approaches, it is just a matter of style.

On the other hand, the other two answers are also correct and are worth taking into account, if only to learn different ways to solve this kind of problems.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: remove last half of a string

Post by flyingDman » 18 Aug 2022, 14:29

if only to learn different ways to solve this kind of problems.
If that's the case let me throw in another one...:

Code: Select all

str := "UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif"
MsgBox, % substr(str, 1, (str~="_Instrumental") - 1)
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”