how to grab part of the string before 'S??E??' ? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
brotherS
Posts: 32
Joined: 15 Apr 2016, 04:27

how to grab part of the string before 'S??E??' ?

25 May 2020, 08:01

I want to use this as part of a script where the input is something like

Some.Text.S01E05.More.Text.Here or
Different.Text.Now.S11E02.Even.More.Text.Here

and the output I want is

Some.Text
Different.Text.Now

It's probably really easy with RegEx, but how?
User avatar
boiler
Posts: 5283
Joined: 21 Dec 2014, 02:44

Re: how to grab part of the string before 'S??E??' ?  Topic is solved

25 May 2020, 08:52

Code: Select all

Text =
(
Some.Text.S01E05.More.Text.Here or
Different.Text.Now.S11E02.Even.More.Text.Here
)

; store matches in array:
Matches := []
loop, Parse, Text, `n, `r
{
	RegExMatch(A_LoopField, ".*(?=\.S\d\dE\d\d)", M)
	Matches.Push(M)
}

; display contents of array:
for Each, Match in Matches
	Out .= Match "`n"
MsgBox, % Out
brotherS
Posts: 32
Joined: 15 Apr 2016, 04:27

Re: how to grab part of the string before 'S??E??' ?

25 May 2020, 09:13

Sorry, I can't get it to work properly, and it might be my fault. What I meant in my post above is that the input is something like

Some.Text.S01E05.More.Text.Here

or

Different.Text.Now.S11E02.Even.More.Text.Here

and the output I want is

Some.Text

or

Different.Text.Now

So there's only ever one line of input.
User avatar
boiler
Posts: 5283
Joined: 21 Dec 2014, 02:44

Re: how to grab part of the string before 'S??E??' ?

25 May 2020, 09:15

Code: Select all

Text := "Some.Text.S01E05.More.Text.Here"
RegExMatch(Text, ".*(?=\.S\d\dE\d\d)", Match)
MsgBox, % Match
User avatar
Chunjee
Posts: 523
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: how to grab part of the string before 'S??E??' ?

25 May 2020, 09:15

brotherS wrote:
25 May 2020, 08:01
It's probably really easy with RegEx, but how?
Use the right Regex pattern

https://regex101.com/ may help visualize
User avatar
boiler
Posts: 5283
Joined: 21 Dec 2014, 02:44

Re: how to grab part of the string before 'S??E??' ?

25 May 2020, 09:18

FYI - My code uses the right RegEx pattern.
brotherS
Posts: 32
Joined: 15 Apr 2016, 04:27

Re: how to grab part of the string before 'S??E??' ?

25 May 2020, 09:52

Thanks, boiler, you're the best! Works perfectly now. :)

Thanks Chunjee, I'll have a look.
brotherS
Posts: 32
Joined: 15 Apr 2016, 04:27

Re: how to grab part of the string before 'S??E??' ?

19 Jun 2020, 11:44

I want to expand the script where I'm using this, how would I still have

Some.Text.S01E05.More.Text.Here

as input and

Some.Text.S01E05

as a secondary output (as another string)?
User avatar
boiler
Posts: 5283
Joined: 21 Dec 2014, 02:44

Re: how to grab part of the string before 'S??E??' ?

19 Jun 2020, 12:28

Code: Select all

Text := "Some.Text.S01E05.More.Text.Here"
RegExMatch(Text, "(.*)\.S\d\dE\d\d", Match)
MsgBox, % Match1 "`n" Match

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], LogRol, nRec and 79 guests