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
Extracting numbers from string Topic is solved
Re: Extracting numbers from string Topic is solved
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
Re: Extracting numbers from string
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.
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.
Re: Extracting numbers from string
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
}
Last edited by swagfag on 19 Sep 2018, 08:01, edited 1 time in total.
Re: Extracting numbers from string
yes, it should be just static i := 1
i dont understand how the incorrect version is parsed, though:
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?
i dont understand how the incorrect version is parsed, though:
Code: Select all
local static i := 1
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?
Re: Extracting numbers from string
i is static. An error would be more appropriate.
Cheers.
Edit: it is an error in v2
Cheers.
Edit: it is an error in v2
Who is online
Users browsing this forum: Araphen and 336 guests