Page 1 of 1

Detect the date from clipboard

Posted: 06 Oct 2019, 08:44
by Qtic
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

Re: Detect the date from clipboard

Posted: 06 Oct 2019, 10:43
by boiler

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.

Re: Detect the date from clipboard

Posted: 06 Oct 2019, 11:56
by flyingDman
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)

Re: Detect the date from clipboard

Posted: 07 Oct 2019, 11:11
by Qtic
Works like a charm, cheers guys!

Re: Detect the date from clipboard

Posted: 10 Oct 2019, 01:47
by Qtic
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.

Re: Detect the date from clipboard

Posted: 10 Oct 2019, 07:29
by boiler
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.

Re: Detect the date from clipboard

Posted: 10 Oct 2019, 09:36
by flyingDman
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.