Regex or similar divide variable Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Regex or similar divide variable

23 Oct 2017, 03:22

Hello,

Can anyone help me to extract these 6 numbers separetly (from notepad .txt), they are inside variable and between each of them are 6-15 space places (with no rules for space places).

I need them in 6 variables.

4.364 4.379 22 9,598090 7 0,052000

Thank you.
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

23 Oct 2017, 03:24

It will be easier if it looks like this.

4.364ssssssss4.379ssssssss22sssssssssssssssss9,598090sssss7ssssssssssssssssssssssss0,052000
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Regex or similar divide variable

23 Oct 2017, 05:26

Try this

Code: Select all

data := "4.364        4.379        22                 9,598090     7                        0,052000"
cur_pos := 1
numbers := []
While(RegExMatch(data, "O)(\d+\.\d+|\d+\,\d+|\d+)", numb, cur_pos)) {
   cur_pos := numb.Pos(1) + numb.Len(1)
   numbers.Insert(numb.Value(1))
}
loop, % numbers.Maxindex() {
   MsgBox, % numbers[a_index]
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

23 Oct 2017, 06:23

Wow!

Odlanir this is it.

Thank you very much.
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

23 Oct 2017, 06:57

Odlanir, please one more question.

If I want extract also letters and numbers or just letters, what will be different?
Lets say that in that varable instead of 22 is GHT22.
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Regex or similar divide variable

23 Oct 2017, 07:10

could be surely better coded by a Regex guru but this should works.

Code: Select all

While(RegExMatch(data, "O)([A-Za-z]*\d+\.\d+|[A-Za-z]*\d+\,\d+|[A-Za-z]*\d+)", numb, cur_pos)) {
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

23 Oct 2017, 07:12

Yes, yes!

Thank you again :)
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex or similar divide variable

23 Oct 2017, 09:09

Code: Select all

data := "4.364        4.379        22                 9,598090     7                        0,052000"
numbers := []
while RegExMatch(data, "O)[\d\.,]+", num, A_Index = 1 ? 1 : num.Pos + num.Len)
   numbers.Push(num[0])

for k, v in numbers
   MsgBox, % v
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Regex or similar divide variable  Topic is solved

23 Oct 2017, 12:58

For something like this I would replace multiple spaces with single spaces, and then use StrSplit, or use a parsing loop.

Code: Select all

q::
vText := "4.364          4.379                22             9,598090              7          0,052000"
MsgBox, % vText
vText := RegExReplace(vText, " \K +")
MsgBox, % vText
;you could trim leading/trailing spaces also
vText := Trim(vText)

;to an array
oArray := StrSplit(vText, " ")
MsgBox, % oArray.1 " " oArray.6

;to variables
Loop, Parse, vText, % " "
	v%A_Index% := A_LoopField
MsgBox, % v1 " " v6
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

24 Oct 2017, 02:14

Guys thanks you all, every answer is exellent.

Ofcourse jeeswg is the master in ahk, logic is great and step by step explanation. :)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Regex or similar divide variable

24 Oct 2017, 02:30

Also, arr:=strsplit(regexreplace(data,"\s+","|"),"|"), example,

Code: Select all

; Input
data := "4.364        4.379        22                 9,598090     7                        0,052000"

arr:=strsplit(regexreplace(data,"\s+","|"),"|")	; Replace any sequence of white space with |, and split the resulting |-delimited string into an array

; Show the result:
for k, num in arr
	str .= "arr[" k "]"  "`t=`t" num "`n" 
msgbox  % str
Cheers.
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

24 Oct 2017, 04:53

hahah, yes i saw :)

Thank you Helgef :)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Regex or similar divide variable

24 Oct 2017, 05:55

@ noname, :lol: :superhappy:
@ jeeswg, I'd use stringsplit rather than the loop if I wanted variables. Good thinking about the trim though :thumbup:
@ hrvoje83, np :wave:

Cheers
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

24 Oct 2017, 06:52

Hi all,

One problem.. in message box everything looks fine.
Lest say first number is 4.364, but when I am put this variable into excel it looks like this 2,364 and that is complety different number.

Code: Select all

XL.Range("A1").Value := v1
result is 2,364 - this is wrong

How to solve that?
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

24 Oct 2017, 06:59

I mean result is 4,364 :) also wrong
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Regex or similar divide variable

24 Oct 2017, 11:44

Removing the dots will that solve it ?
btw 4.364 does that mean four thousand three hundred sixty four ? ( in your country notation)

Code: Select all

; Input
data := "4.364        4.379        22                 9,598090     7                        0,052000"

arr:=strsplit(regexreplace(strReplace(data,"."),"\s+","|"),"|")	; Replace any sequence of white space with |, and split the resulting |-delimited string into an array

; Show the result:
for k, num in arr
	str .= "arr[" k "]"  "`t=`t" num "`n" 
msgbox  % str
hrvoje83
Posts: 45
Joined: 01 Mar 2017, 10:37

Re: Regex or similar divide variable

27 Oct 2017, 02:19

Ni noname,

Sorry I was away for two days.

Yes good thinking, first to replate dots. Thanks.

Thats right, in my country this is 4.000 four thousand.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Rohwedder and 205 guests