Replacing numbers (years) in a string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Art365
Posts: 15
Joined: 25 Aug 2017, 08:53

Replacing numbers (years) in a string

Post by Art365 » 25 Aug 2017, 09:11

Edit: follow-up question in post 4.

Hello,

I'm new to autohotkey but starting to discover it's great potential! I have a problem I'm trying to solve: I need to increment a year in a string of numbers, such that, for example, 20160930 becomes 20170930. To that end I've written (well, more like assembled out of other people's solutions) this script:

Code: Select all

Capslock & d::
{
Sleep, 50
Send ^c
Sleep, 50
RegExMatch(clipboard, "^[201]\d{1}$", Number)
Sleep, 50
NewNumber:=Number1+1
Sleep, 50
Clipboard:=RegExReplace(clipboard, Number1, NewNumber)
Sleep, 50
Send ^v
Sleep, 50
}
Return
However, it doesn't work. It just pastes the same string of numbers you copied originally. Would greatly appreciate some help.
Last edited by Art365 on 26 Aug 2017, 16:24, edited 1 time in total.

Nightwolf85
Posts: 302
Joined: 05 Feb 2017, 00:03

Re: Replacing a number (year) in a string  Topic is solved

Post by Nightwolf85 » 25 Aug 2017, 09:42

I'm sure there is a better way since the format is standard, but this is similar to what you have:

Code: Select all

Capslock & d::
clipboard := ""
Send, ^c
ClipWait, 1
IF (ErrorLevel)
	Return
temp := clipboard
temp := RegExReplace(temp, "(.*?)([0-9]{4})(.*)", "$1_$2_$3")
temp := StrSplit(temp, "_")
clipboard := temp[1] . (temp[2] ? Format("{1:04d}", temp[2] + 1) : "") . temp[3]
Sleep 50
Send, ^v
Return
Edit: Changed to add year to any date (4 digit number) in highlighted string, while keeping the rest of the string in tact. Also if the number starts with a 0s they will be kept, if no 4 digit number is found sting isn't changed.
Last edited by Nightwolf85 on 25 Aug 2017, 09:55, edited 1 time in total.

Art365
Posts: 15
Joined: 25 Aug 2017, 08:53

Re: Replacing a number (year) in a string

Post by Art365 » 25 Aug 2017, 09:54

Awesome! It works. Thank you!!

Art365
Posts: 15
Joined: 25 Aug 2017, 08:53

Re: Replacing a number (year) in a string

Post by Art365 » 26 Aug 2017, 15:26

Now I have another question, related to this same function. I want the script to increment multiple years in selection (years from 2015 to 2019), if there is more than one there. Once again I've assembled something from other people's solutions, but it doesn't quite work. Would appreciate any feedback.

Code: Select all

^+i::
{
    clipboard := ""
    Send ^c
    ClipWait, 1
    IF (ErrorLevel)
        Return
    selection := clipboard
    pos := 1
    while pos := RegExMatch(selection, "(201*[5-9])", year, pos+StrLen(year))
    {
        newyear := year + 1
	selection:=RegExReplace(selection, "(201*[5-9])", newyear, , ,pos+StrLen(year))
    }
    Clipboard:= selection
    Sleep, 50
    Send ^v
}
Return
Edit: I got it!

Code: Select all

^+i::
{
    clipboard := ""
    Send ^c
    ClipWait, 1
    IF (ErrorLevel)
        Return
    selection := clipboard
	pos := 1
    while pos := RegExMatch(selection, "(201*[5-9])", year, pos)
	{
	    newyear := year + 1
	    selection:=RegExReplace(selection, "(201*[5-9])", newyear,, 1, pos)
            pos += StrLen(newyear)
	}
	Clipboard:=selection
    Sleep, 50
    Send ^v
}
Return

Post Reply

Return to “Ask for Help (v1)”