getting text between two words in a string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
awcrt9316
Posts: 61
Joined: 03 Mar 2020, 20:06

getting text between two words in a string

Post by awcrt9316 » 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: 1472
Joined: 05 May 2018, 12:23

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

Post by AHKStudent » 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: 16769
Joined: 21 Dec 2014, 02:44

Re: getting text between two words in a string

Post by boiler » 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 "<<"

User avatar
zvit
Posts: 224
Joined: 07 Nov 2017, 06:15

Re: getting text between two words in a string

Post by zvit » 20 Jan 2022, 13:05

AHKStudent wrote:
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
This works but I'm curious as to how. The regexmax function is putting the result into a var called "results" and the msgbox is using a var called "results2" which technically is a var that was never used in the code. So, how is the var "results2" getting the value?

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: getting text between two words in a string

Post by sofista » 20 Jan 2022, 14:23

zvit wrote:
20 Jan 2022, 13:05
AHKStudent wrote:
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
This works but I'm curious as to how. The regexmax function is putting the result into a var called "results" and the msgbox is using a var called "results2" which technically is a var that was never used in the code. So, how is the var "results2" getting the value?

In variable results —technically is a pseudoarray— is stored the complete match. In results1 is stored the first captured group —a group is the string surrounded by ()— and in results2 is stored the second captured group, as follows:

Code: Select all

results : |me  This is a test |
results1: |me|
results2: |  This is a test |
Details in RegExMatch.

Edited for clarity.

NeilW
Posts: 13
Joined: 16 Aug 2021, 21:31

Re: getting text between two words in a string

Post by NeilW » 08 May 2022, 23:31

For those who got confused about the expression above, you could try this.

Code: Select all

FCONTENT.="ME THIS IS A TEST APPLE"
RegExMatch(FCONTENT, "ME\K.*(?=APPLE)", TEST)
msgbox,%TEST%
"\K.*" means grab all the text after "ME"

"(?=APPLE)" means look for texts before the word "APPLE"

User avatar
boiler
Posts: 16769
Joined: 21 Dec 2014, 02:44

Re: getting text between two words in a string

Post by boiler » 09 May 2022, 01:10

@NeilW — In other words the same as this post, except yours leaves spaces around the matched text.

Post Reply

Return to “Ask for Help (v1)”