Best way to increase a number in a string

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Best way to increase a number in a string

Post by Exaskryz » 26 Apr 2016, 17:21

Code: Select all

clipboard:="my.website.ahk/page/1"
;Loop 5
clipboard:=RegExReplace(clipboard,"(\d+)/?$",RegExMatch(clipboard,"\d+",match)?match+1:"error")
MsgBox % clipboard
return
I want to return "my.website.ahk/page/2", and then /3, then /4, etc. The code above works, but it looks so messy. I wish there was a way to just do clipboard:=RegExReplace(clipboard,"(\d+)/?$",$1+1) and treating the $1 like a variable. Maybe there is some regex trick I'm not aware of, or maybe RegEx isn't the way to go about this at all?

timelizards
Posts: 20
Joined: 11 Sep 2015, 21:00

Re: Best way to increase a number in a string

Post by timelizards » 26 Apr 2016, 17:51

Code: Select all

num := 0
string := "beginning.of.string\"

Loop, 5
	MsgBox % string . (num := num + 1)

timelizards
Posts: 20
Joined: 11 Sep 2015, 21:00

Re: Best way to increase a number in a string

Post by timelizards » 26 Apr 2016, 17:54

i realize my solution doesnt use the clipboard, but i was trying to answer the question about increasing the number in a string. with the example you provided, i would prolly knock the number of the end of the string, analyze it if i needed it, and append my own variable to the end of the string the way my solution shows

wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Best way to increase a number in a string

Post by wolf_II » 26 Apr 2016, 18:21

Try this:

Code: Select all

String := "my.website.ahk/page/1"
Loop, 5
    MsgBox, % Inc(String)
ExitApp


;-------------------------------------------------------------------------------
Inc(ByRef String) {
;-------------------------------------------------------------------------------
    RegExMatch(String, "(?<Head>.*)(?<Num>\d+)/?$", $)
    Return, String := $Head . ++$Num
}
I hope that helps.

User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Best way to increase a number in a string

Post by Capn Odin » 26 Apr 2016, 18:27

This example does not work, but maybe it is possible to implement in this fashion.

Code: Select all

num := 1

clipboard := "my.website.ahk/page/1"

#s::
	clipboard := RegExReplace(clipboard, "(\d+)(?C:NumAddOne)", num)
	ToolTip, % clipboard
return

NumAddOne(Match){
	Global num
	if(Match is number) {
		num := Match + 1
		return 0
	}
	return 1
}
Please excuse my spelling I am dyslexic.

User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Best way to increase a number in a string

Post by Exaskryz » 26 Apr 2016, 18:29

This post was crafted prior to wolf's post.

Yeah, I don't think it's that tough if I know the location of the digit, but there's no guarantee the length of the URL stays constant.

My real code is this:

Code: Select all

NumpadSub::
Send ^l^c
clipboard:=RegExReplace(clipboard,"(\d+)/?$",RegExMatch(clipboard,"\d+",match)?match+1:"error")
Send ^l^v{Enter}
return
Which will update the URL for me.

I could break it into two lines by just executing the RegExMatch() above first, then throwing match+1 in there right away (possibly with an If immediately preceding the RegExMatch(), as that would return false if no number found..), so.. like this:

Code: Select all

NumpadSub::
Send ^l^c
If RegExMatch(clipboard,"\d+/?$",match) ; I did change the end-of-line anchor and optional / which wasn't in my original code
clipboard:=RegExReplace(clipboard,"\d+/?$",match+1)
Send ^l^v{Enter}
return
That's functional, and I guess it makes it shorter technically -- by 5 characters before further improvements* -- and more readable, so I'll stick with that for now. But still hoping there might be an even shorter way, particularly when you might want to increment multiple numbers in a string (not a use case I have right now, but someone else might have had it or have it in the future).

*Talking about adding the /?$ in RegExMatch(), and dropping the () in the RegExReplace() as I don't need a captured subpattern with this method. I could further make it better by assigning my needle string to a variable in RegExMatch() and using that in the RegExReplace().

---

Edit: In response to wolf, that looks like a great solution. I didn't consider breaking apart the string like that, manipulating one side of it, and then piecing it back together.

Edit: In response to Odin, that also seems like a fun solution. Yours might have better application with the call to a function (had no idea regex could do that honestly, glad it can) that I believe could then be used on any number of digits in a string (if they were to be incremented simultaneously). Like I mentioned in this same post above, I don't have a purpose for that, but someone else might.

Edit: For Odin again, here's some code that gets really close to fixing your idea.

Code: Select all

^2::
Loop 5
{
clipboard := "my.website.ahk/page/" A_Index
	clipboard := RegExReplace(clipboard, "(\d+)(?C:NumAddOne)", num)
	ToolTip, % clipboard
	Sleep 1000
}
return
 
NumAddOne(Match){
	Global num
	if Match is number ; can't have the OTB style, nor do you want this as an expression with the parentheses that you had before
	{
		num := Match + 1
		return 0
	}
	return 1
}
The problem here is the replacement happens before the function is called from what I can tell. Because I get my.website.ahk/page/, then my.website.ahk/page/2, then /3, then /4, then /5. You would expect that /6 is the final result in the tooltip, but it's not, even if num has that value. Actually, you can verify this by making the tooltip ToolTip, % clipboard "`n" num). The num is always one greater than what is in the URL.

Post Reply

Return to “Ask for Help (v1)”