Extracting numbers from string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Extracting numbers from string

18 Sep 2018, 13:34

Hi All,

I have a string which I require to extract the numerical valuses from.
Here is an example of the string:
"149 APPLES 1205 ORANGES AND 151 APPLES 1263 ORANGES"

I would like to extract it as follows:
variable 1 = 149
variable 2 = 1205
variable 3 = 151
variable 4 = 1263

Some things to consider:
The format is always the same."X APPLES X ORANGES AND X APPLES X ORANGES"
Each numbers length can vary from 1 digit to 4 digits long.

Hope this makes sense to someone.

Regards,

jmcall10
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Extracting numbers from string  Topic is solved

18 Sep 2018, 13:48

Code: Select all

#SingleInstance, Force

TestString := "149 APPLES 1205 ORANGES AND 151 APPLES 1263 ORANGES"

Pos := 1

While Pos := RegExMatch(TestString, "(\d+)", Match, Pos + StrLen(Match)) {
	Variable%A_Index% := Match
}

Result =
(LTrim
	Variable 1 = %Variable1%
	Variable 2 = %Variable2%
	Variable 3 = %Variable3%
	Variable 4 = %Variable4%
)

MsgBox, % Result
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: Extracting numbers from string

18 Sep 2018, 14:00

I have managed to achieve what I was after using a different approach (most likely not a very good approach)

MyString := "149 APPLES 1205 ORANGES AND 151 APPLES 1263 ORANGES"
word_array := StrSplit(MyString , A_Space, ".") ; Omits periods.
MsgBox % "The 1st number is" word_array[1]
MsgBox % "The 2nd number is" word_array[3]
MsgBox % "The 3rd number is" word_array[6]
MsgBox % "The 4th number is" word_array[8]

This only works if the format of the string never changes.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Extracting numbers from string

18 Sep 2018, 15:15

Code: Select all

string := "149 APPLES 1205 ORANGES AND 151 APPLES 1263 ORANGES"
RegExMatch(string, "\b\d+\b(?CmakeVars)")

makeVars(match) {
	global

	static i := 1

	variable%i% := match
	++i

	return 1
}
edit: fix local static i := 1 to static i := 1. see post below
Last edited by swagfag on 19 Sep 2018, 08:01, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Extracting numbers from string

19 Sep 2018, 05:42

This doesn't do what you think,

Code: Select all

local static i := 1
Check listvars.

Cheers.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Extracting numbers from string

19 Sep 2018, 08:00

yes, it should be just static i := 1
i dont understand how the incorrect version is parsed, though:

Code: Select all

local static i := 1
local static declares a blank local variable called 'static'
so what is i then? its not global, since it doesnt leak. its not pure local, since it gets incremented between function calls. so its static then, but if the keyword 'static' in this context is the name of a var, what tells i to become a static var?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Extracting numbers from string

21 Sep 2018, 02:58

i is static. An error would be more appropriate.

Cheers.

Edit: it is an error in v2 :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: fiendhunter and 246 guests