Extracting text from a line where the last occurence of a string occurs.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Extracting text from a line where the last occurence of a string occurs.

20 Jan 2021, 22:10

In this archived thread https://autohotkey.com/board/topic/68680-regexmatch-backwards/ member bobycom describes that he uses InStr(FileContents, "Score", , 0) to find the last occurence of text in a file. He then says 'So now I'm using InStr to find the line needed and then RegExmatch to extract the values I want.'

This is more or less what I want do, but as I am a relative newbie to Autohotkey, I'm not sure how to do. Can anyone assist please?
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Extracting text from a line where the last occurence of a string occurs.

20 Jan 2021, 22:15

You provided the code in your post. It gets the starting position of the last instance.

Code: Select all

MsgBox, % InStr("ScoreScoreScore", "Score",, 0) 
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Re: Extracting text from a line where the last occurence of a string occurs.

20 Jan 2021, 22:59

I don't know if I didn't understand you correctly or didn't explain what I was looking for exactly or both.

So I'll give a simple example adapted from original thread.

Code: Select all

; Dummy data
FileContents = 
(Join`r`n
I want to find some text in a line at a point before where the last string occurs, 
say that string happens to be ice-cream, and say the text I want to return is in format xxx line.
This is the 3rd line, 2nd ice-cream
this is the 4th line, 3rd ice-cream
this is the 5th line. 4th and last ice-cream
this is the 5th line
this is the last line
)
[Mod edit: [code][/code] tags added.]

In the original example https://autohotkey.com/board/topic/68680-regexmatch-backwards/ member Kon uses regex to extract the last number that occurs after the text "Score",
but in my example I want to extract text "5th line" or actually even that complete 5th line would be ok.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 00:47

Code: Select all

word := "ice-cream"
FileContents = 
(Join`r`n
I want to find some text in a line at a point before where the last string occurs, 
say that string happens to be ice-cream, and say the text I want to return is in format xxx line.
This is the 3rd line, 2nd ice-cream
this is the 4th line, 3rd ice-cream
this is the 5th line. 4th and last ice-cream
this is the 5th line
this is the last line
)
loop, parse, filecontents, `n
{
	if instr(a_loopfield, word)
		foundIt := A_LoopField
}
msgbox, % foundIt
ExitApp
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 01:10

@AHkstudent thanks for your reply and apologies that I didn't specify, I was trying to avoid using a loop if possible, as that has to read every line, and I want to avoid that overhead if possible.
From my limited understanding and from the archived thread https://autohotkey.com/board/topic/68680-regexmatch-backwards/ that I mentioned at the start I believe it can be done without using a loop.
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 02:27

Code: Select all

; Dummy data
FileContents = 
(Join`r`n
I want to find some text in a line at a point before where the last string occurs, 
say that string happens to be ice-cream, and say the text I want to return is in format xxx line.
This is the 3rd line, 2nd ice-cream
this is the 4th line, 3rd ice-cream
this is the 5th line. 4th and last ice-cream
this is the 5th line
this is the last line
)
MsgBox, % RegExReplace(FileContents, "s).*(?=5th\V*)|\v.*")
MsgBox, % RegExReplace(FileContents, "s).*\v(?=\V*5th\V*)|\v.*")
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 03:35

@teadrinker Thanks very much for your help, this is pretty much what I was looking for and is enough for me to work with. :D

If I specifically wanted words that occured between two strings here. Say the words that occured between "Reporter:" and "likes ice-cream" in the sample text below, and only the last occurence. So in this case the outputted text should be "The big bad wolf", any idea how to do that?

Reporter: Dave likes ice-cream
Reporter: Everybody likes ice-cream
Reporter: The big bad wolf likes ice-cream
Reporter: somebody likes cake
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 07:31

bazkeys wrote:
21 Jan 2021, 03:35
If I specifically wanted words that occured between two strings here. Say the words that occured between "Reporter:" and "likes ice-cream" in the sample text below, and only the last occurence. So in this case the outputted text should be "The big bad wolf", any idea how to do that?
Maybe the following regex gets what you want:

Code: Select all

SampleText := "
(
Reporter: Dave likes ice-cream
Reporter: Everybody likes ice-cream
Reporter: The big bad wolf likes ice-cream
Reporter: somebody likes cake
)"

Begin  := "Reporter: "
End    := " likes ice-cream"
Needle := ".*" Begin "(.*?)" End

RegExMatch(SampleText, Needle, m)
MsgBox, % m1

; Output -> The big bad wolf
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 12:38

@sofista
Try your code with

Code: Select all

SampleText =
(Join`r`n
Reporter: Dave likes ice-cream
Reporter: Everybody likes ice-cream
Reporter: The big bad wolf likes ice-cream
Reporter: somebody likes cake
)
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 13:15

teadrinker wrote:
21 Jan 2021, 12:38
@sofista
Try your code with

Code: Select all

SampleText =
(Join`r`n
Reporter: Dave likes ice-cream
Reporter: Everybody likes ice-cream
Reporter: The big bad wolf likes ice-cream
Reporter: somebody likes cake
)
I see. If that is the case, then the needle should be:

Code: Select all

Needle := "s).*" Begin "(.*?)" End
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 18:23

@sofista Thanks for your reply, I tried that code with the edit and it outputs
Reporter: Dave likes ice-cream
Reporter: Everybody likes ice-cream
Reporter: The big bad wolf likes ice-cream

but the desired output is 'The big bad wolf'
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Extracting text from a line where the last occurence of a string occurs.

21 Jan 2021, 19:44

bazkeys wrote:
21 Jan 2021, 18:23
@sofista Thanks for your reply, I tried that code with the edit and it outputs
Reporter: Dave likes ice-cream
Reporter: Everybody likes ice-cream
Reporter: The big bad wolf likes ice-cream

but the desired output is 'The big bad wolf'
Don't know what's going on, with this code I get the desired output running Windows 7, AutoHotkey 1.1.33.02:

Code: Select all

SampleText =
(Join`r`n
Reporter: Dave likes ice-cream
Reporter: Everybody likes ice-cream
Reporter: The big bad wolf likes ice-cream
Reporter: somebody likes cake
)

Begin  := "Reporter: "
End    := " likes ice-cream"
Needle := "s).*" Begin "(.*?)" End

RegExMatch(SampleText, Needle, m)
MsgBox, % m1 ; The big bad wolf

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google Adsense [Bot] and 304 guests