Parsing a string issue

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tecdragon
Posts: 14
Joined: 31 Aug 2020, 10:22

Parsing a string issue

17 Sep 2021, 16:23

I am trying to get the last 5 digits of the hostname that is presented as such 20-91 or 34-87, etc. The following code shows the last for but when I try to parse the "-" out of it, it doesn't work. why?

Code: Select all

hostname = %A_computername%
String = %hostname%

msgbox,string %String%

input = % SubStr(String, -4)

msgbox, input %input%
MsgBox % RegExReplace(input, "[-]")
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Parsing a string issue

17 Sep 2021, 16:57

what is the input??

Code: Select all

MsgBox % input := SubStr("askdhgfjsd34-87", -4) ; "34-87"
MsgBox % RegExReplace(input, "[-]") ; 3487
works fine
tecdragon
Posts: 14
Joined: 31 Aug 2020, 10:22

Re: Parsing a string issue

20 Sep 2021, 08:47

In the example I am trying to get the hostname of the computer, save it as a string and then parse out what I want as the variable 'input' I then try to parse the 'input' which is the hostname of the computer and remove the '-' everything works up to removing the '-'
User avatar
mikeyww
Posts: 26856
Joined: 09 Sep 2014, 18:38

Re: Parsing a string issue

20 Sep 2021, 09:08

RegExReplace returns a value but does not reassign your variable to it. You can do that by adding input := .
tecdragon
Posts: 14
Joined: 31 Aug 2020, 10:22

Re: Parsing a string issue

20 Sep 2021, 11:56

I changed this up a bit to be more reflective of what I am trying to do, still does not parse the letters from the final msgbox.

Code: Select all

hostname = %A_computername% 
;Sets the computer name as variable hostname

msgbox,HostName %hostname% 
;displayd the hostname in my case Thundera

hostname := % SubStr(hostname, -4) 
;Parses the last five 'ndera' and changes the hostname variable to 'ndera'

msgbox, Hostname %hostname% 
;Shows 'ndera' as expected

MsgBox % RegExReplace(hostname, "[er-]")
;does not parse out the 'er-' from the name

Return
tecdragon
Posts: 14
Joined: 31 Aug 2020, 10:22

Re: Parsing a string issue

20 Sep 2021, 12:18

Think I figured this out, or at least a workaround using string replace

Code: Select all

hostname = %A_computername%
String = %hostname%
stringreplace, string1,string, e,, all
msgbox,string %String%
msgbox,string1 %string1%
input = % SubStr(String1, -4)
msgbox, input %input%

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot], hiahkforum, jchestnut, mcd, Sem552 and 131 guests