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?
Extracting text from a line where the last occurence of a string occurs.
Re: Extracting text from a line where the last occurence of a string occurs.
You provided the code in your post. It gets the starting position of the last instance.
Code: Select all
MsgBox, % InStr("ScoreScoreScore", "Score",, 0)
Re: Extracting text from a line where the last occurence of a string occurs.
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.[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.
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
)
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.
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Extracting text from a line where the last occurence of a string occurs.
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
Re: Extracting text from a line where the last occurence of a string occurs.
@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.
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.
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Extracting text from a line where the last occurence of a string occurs.
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.*")
Re: Extracting text from a line where the last occurence of a string occurs.
@teadrinker Thanks very much for your help, this is pretty much what I was looking for and is enough for me to work with.
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
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
Re: Extracting text from a line where the last occurence of a string occurs.
Maybe the following regex gets what you want:bazkeys wrote: ↑21 Jan 2021, 03:35If 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?
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
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Extracting text from a line where the last occurence of a string occurs.
@sofista
Try your code with
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
)
Re: Extracting text from a line where the last occurence of a string occurs.
I see. If that is the case, then the needle should be:teadrinker wrote: ↑21 Jan 2021, 12:38@sofista
Try your code withCode: 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 )
Code: Select all
Needle := "s).*" Begin "(.*?)" End
Re: Extracting text from a line where the last occurence of a string occurs.
@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'
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'
Re: Extracting text from a line where the last occurence of a string occurs.
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
Who is online
Users browsing this forum: Google Adsense [Bot] and 304 guests