Detect the date from clipboard

Get help with using AutoHotkey and its commands and hotkeys
Qtic
Posts: 3
Joined: 06 Oct 2019, 08:13

Detect the date from clipboard

06 Oct 2019, 08:44

Hello,

I'm having troubles extracting the date "DD.MM" inside my clipboard correctly. My clipboard typically looks like this:
Ja NBS 26.09.2019 123456789 Team Auto FIRSTNAME LASTNAME K12345 A RI NOK 4.656,04
In this case I can get the text using:

Code: Select all

oldestdate:= SubStr(clipboard, 8, 5)
Trouble is the length of the text before varies. Instead of "Ja NBS" it can for example be "Nei INAR" or several different length of characters, meaning that the date isn't at a fixed place in the clipboard. I am admittedly pretty new to AHK, so I'm wondering if there is any way I can make sure that I get the date extracted from the clipboard correctly each time?

There are never any digits before the actual date, so the first thing that comes to mind is something like:

Code: Select all

oldestdate:= SubStr(clipboard, FIRST INSTANCE OF A DIGIT, 5)
But I don't know how to make this work..

Thanks for any input :D
User avatar
boiler
Posts: 2914
Joined: 21 Dec 2014, 02:44

Re: Detect the date from clipboard

06 Oct 2019, 10:43

Code: Select all

RegExMatch(Clipboard, "\d+\.\d+\.\d+", oldestdate)
Edit: Just realized you only want day and month, so my version becomes:

Code: Select all

RegExMatch(Clipboard, "\d+\.\d+", oldestdate)
Or more safely:

Code: Select all

RegExMatch(Clipboard, "\d{2}\.\d{2}", oldestdate)
These would find it even if it turns out it's not the first digit in the string.
Last edited by boiler on 06 Oct 2019, 12:05, edited 2 times in total.
User avatar
flyingDman
Posts: 291
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Detect the date from clipboard

06 Oct 2019, 11:56

FIRST INSTANCE OF A DIGIT can be written as var~="\d" so your own code becomes

Code: Select all

oldestdate:= SubStr(clipboard, clipboard~="\d", 5)
Qtic
Posts: 3
Joined: 06 Oct 2019, 08:13

Re: Detect the date from clipboard

07 Oct 2019, 11:11

Works like a charm, cheers guys!
Qtic
Posts: 3
Joined: 06 Oct 2019, 08:13

Re: Detect the date from clipboard

10 Oct 2019, 01:47

Hello,

I have another similar cases where the clipboard can look like these:

R2 01.10.2019 13213212 XXXXXXXX K49500 T A RI NOK 102.407,31
R1 01.10.2019 13213212 XXXXXXXX K49500 T A RI NOK 131.169,05
R23 02.10.2019 13213212 XXXXXXXX K49500 T A RI NOK 202.527,14

Meaning that there is either one or 2 digits before the date i want to select. Here I think this can be solved by finding the first "." and getting the date by going 2 positions backward, then copying the next 10 characters.

Something like:

Code: Select all

oldestdate:= SubStr(clipboard, clipboard~="\"."", -2,  5)
This code is obviously nonsense and doesn't work, I just hope it aids in making my problem understood.

Does someone know how to work this out?

Thanks in advance.
User avatar
boiler
Posts: 2914
Joined: 21 Dec 2014, 02:44

Re: Detect the date from clipboard

10 Oct 2019, 07:29

It's already solved by using my version. Use the first one if you want the whole date including year. Use one of the other two if you want just the month and day.
User avatar
flyingDman
Posts: 291
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Detect the date from clipboard

10 Oct 2019, 09:36

In my notation it would become:

Code: Select all

msgbox % oldestdate:= SubStr(clipboard, clipboard~="\d{2}\.\d{2}", 5)
I.e. search on and return the position of the first occurrence of a pattern consisting of 2 pairs of digits separated by a period. But this would work as well:

Code: Select all

oldestdate:= SubStr(clipboard, (clipboard~="\.") - 2, 5)
Note: the regex is the same. The only thing I do is to use a shortcut notation for the regex allowing you to make this a oneliner.

Return to “Ask For Help”

Who is online

Users browsing this forum: flyingDman, Google [Bot], netor9991, r4961, TAC109 and 236 guests