remove last half of a string
remove last half of a string
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.
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.
Re: remove last have of a string
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
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: remove last have of a string
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
Re: remove last half of a string
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.
Re: remove last half of a string
Code: Select all
str:="UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif"
MsgBox % StrSplit(str, "_Instrumental").1
Re: remove last half of a string
@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:
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.
Code: Select all
str := "UPM_Tru154_2_Beautiful_Moment_Instrumental_Gilbert_Hampson_Gilbert_1512122.wav_Stereo.aif"
MsgBox, % (m1, RegExMatch(str, "^(.*)_Instrumental", m))
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.
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: remove last half of a string
If that's the case let me throw in another one...:if only to learn different ways to solve this kind of problems.
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