getting text between two words in a string Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
awcrt9316
Posts: 61
Joined: 03 Mar 2020, 20:06

getting text between two words in a string

16 May 2020, 10:56

I am trying to store in a variable, a word which is between two other words. For example between "Me" and "apples":

Code: Select all

string := "Me	eat	apples	hungry           yes" ; i would like to store the word "eat" in a variable
The thing is that the space between words may vary, but the two words that would stay the same are "Me" and "apples", and i want to get what ever word between them.

I know how to get the second word of an array this way

Code: Select all

TestString := "This is a test."
word_array := StrSplit(TestString, A_Space) 
MsgBox % "The 2nd word is " word_array[2]
But if there is two words between me and apples, how do i get the stuff between
AHKStudent
Posts: 912
Joined: 05 May 2018, 12:23

Re: getting text between two words in a string  Topic is solved

16 May 2020, 11:44

I suspect regex is the answer, im no good at regex, but here is the idea

Code: Select all

TestString := "me  This is a test apple."

regexmatch(teststring, "(me)(.*?)(?=apple)", results)
msgbox, % results2
User avatar
boiler
Posts: 6594
Joined: 21 Dec 2014, 02:44

Re: getting text between two words in a string

16 May 2020, 12:39

Slight mod to not include spaces before and after the words of interest:

Code: Select all

testString := "me  This is a test apple."

RegExMatch(testString, "me *\K.*?(?= *apple)", results)
MsgBox, % ">>" results "<<"

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Haan, jongyun24, Recrix, takenbythedesert and 52 guests