REGEX - can not retrive number from string (script one line) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

REGEX - can not retrive number from string (script one line)

22 Sep 2021, 04:45

Hello,

I was checking Regex website and tried to get from this string "Order-number: 123456464-" only number with minus sig → 12345664-
On website its working, but it does not work in AHK.

Could some one give me help with this little challenge?

Code: Select all

; I need assign to variable only value which contains numbers and minus in the end.  output: 124546464-
MsgBox % RegExMatch("Kunde-number: 124546464-", "\d+-", OutputVar) 
MsgBox % OutputVar 

; Also tried 
Msgbox,%   RegExReplace("Kunde-number: 124546464-", "[^\d]+") ; output: 124546464 but I need minus too -

MsgBox % RegExReplace(("Kunde-number: 124546464-", "[^0-9-]") ; output: -124546464- , but i need only last minus - example 124546464-
Attachments
regex example.JPG
regex example.JPG (11.49 KiB) Viewed 331 times
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: REGEX - can not retrive number from string (script one line)  Topic is solved

22 Sep 2021, 05:58

Code: Select all

str = Kunde-number: 124546464-
MsgBox, 64, Result, % new := RegExReplace(str, ".+: *")
Or:

Code: Select all

str = Kunde-number: 124546464-
RegExMatch(str, "\d+-", new)
MsgBox, 64, Result, %new%

Code: Select all

MsgBox, 64, Kunde-number, % kn("Kunde-number: 124546464-")

kn(str) {
 RegExMatch(str, "\d+-", new)
 Return new
}
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: REGEX - can not retrive number from string (script one line)

22 Sep 2021, 06:06

@DanRim - Why do you think your first try didn’t work? Is it because you saw only the first MsgBox, which is the return value of the RegExMatch (the position of the found string) and didn’t wait for the second MsgBox to see the matched string?
DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

Re: REGEX - can not retrive number from string (script one line)

22 Sep 2021, 07:31

@mikeyww Thank you for2 examples! It worked. I will google what does it mean → ".+: *"

@boiler Good question and thank you for that , because I checked once again. It seems I used wrong syntax. I work with 2 pc, and on my laptop i used right syntax, but on my work pc I used wrong syntax. Really lost here :D

The problem was:
Example:
I had variable := document.getElementById("something").innerHTML, I assignined myVariable2 := RegExMatch( variable, "\d+-"), but I did not add the third parameter which creates new variable as in example OutputVar. So I just lost lost myself in syntax. Totally rookie mistake.
Now I going though some small tutorials on Youtube with Juho Lee. He really makes good tutorials and I hope learn more.

Code: Select all

MsgBox % RegExMatch("Kunde-number: 124546464-", "\d+-", OutputVar) 
MsgBox % OutputVar 
Thank you guys for examples and for making me think :)
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: REGEX - can not retrive number from string (script one line)

22 Sep 2021, 08:19

No problem. boiler is right; I just stole your code and removed the MsgBox command to make it work. :)

.+: * is: one or more non-line-break characters (probably more accurately stated as carriage return instead of line feed, in the case of AHK), followed by colon, followed by zero or more spaces. . is formally defined as
any single character except `r in a newline (`r`n) sequence.
Explained: RegEx

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, dinodragon, mcd and 154 guests