regexmatch from clipboard

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

regexmatch from clipboard

04 Sep 2019, 19:42

Hello, I really need help with this, thank you in advance

clipboard item is below ;
clip := clipboard

Get an appointment
07-Sep-19, Saturday 02:00pm - 04:00pm
That line changes everytime with different information but still presents 17:00
07-Sep-19, Saturday 03:00pm - 05:00pm
That line changes everytime with different information but still presents 18:00
08-Sep-19, Sunday 08:00am - 10:00am
That line changes everytime with different information but still presents 11:00
08-Sep-19, Sunday 01:00pm - 03:00pm
That line changes everytime with different information but still presents 16:00
08-Sep-19, Sunday 02:00pm - 04:00pm
That line changes everytime with different information but still presents 17:00

I am trying to get time and date separately as a variable for each option.
Date1; 07-Sep-19, Saturday
time1 ; 02:00pm - 04:00pm

Date2: 07-Sep-19
Time2: 03:00pm - 05:00pm

by this way, I will make a script if the dates are the same or different.


RegExMatch(clip, "O)Get an appointment \r\n(?<dat1>.*)\,\n(?<tim1>.*)\r\n(?<dur>.*)\r\n(?<dat2>.*)\,\n(?<tim2>.*)\r\n", apt)

dat11 := % apt["dat1"]
tim11 := % apt["tim1"]
dat22 := % apt["dat2"]
tim22 := % apt["tim2"]
dur := % apt["dur"]

If dat11 = % dat22
Msgbox, " . dat11 . ", between " . tim11 . " or " . tim22 . " ?
If dat11 != % dat22
Msgbox, " . dat11 . ", between " . tim11 . " or " . dat22 . " " . tim22 . " ?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: regexmatch from clipboard

04 Sep 2019, 20:43

This captures the dates and times into arrays:

Code: Select all

Clip =
(
Get an appointment 
07-Sep-19, Saturday 02:00pm - 04:00pm 
That line changes everytime with different information but still presents 17:00 
07-Sep-19, Saturday 03:00pm - 05:00pm 
That line changes everytime with different information but still presents 18:00 
08-Sep-19, Sunday 08:00am - 10:00am 
That line changes everytime with different information but still presents 11:00 
08-Sep-19, Sunday 01:00pm - 03:00pm 
That line changes everytime with different information but still presents 16:00 
08-Sep-19, Sunday 02:00pm - 04:00pm 
That line changes everytime with different information but still presents 17:00 
)

Date := []
Time := []
StartPos := 1

loop
{
	FoundPos := RegExMatch(clip, "O)(\d{2}-\w{3}-\d{2}, \w+) (\d{2}:\d{2}\w{2} - \d{2}:\d{2}\w{2})", Match, StartPos)
	if !FoundPos
		break
	Date.Push(Match[1])
	Time.Push(Match[2])
	StartPos := FoundPos + StrLen(Match[1]) + StrLen(Match[2])
}

; show captured data:
loop, % Date.Count()
	TextOut .= Date[A_Index] " >> " Time[A_Index] "`n"
MsgBox, %TextOut%
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

Re: regexmatch from clipboard

04 Sep 2019, 21:17

Thank you for the reply, I tried this but it shows empty message box for some reason.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: regexmatch from clipboard

04 Sep 2019, 21:30

What version of AHK are you using? You could run this script to find out:

Code: Select all

MsgBox % A_AhkVersion
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

Re: regexmatch from clipboard

04 Sep 2019, 21:36

version;
1.1.23.01
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: regexmatch from clipboard

04 Sep 2019, 21:38

You can either update to the latest version, or this change should work:

From:
loop, % Date.Count()

To:
loop, % Date.MaxIndex()
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

Re: regexmatch from clipboard

04 Sep 2019, 21:42

I tried with the new version;
it gives this result;

Date[A_Index] " >> " Time[A_Index] "
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

Re: regexmatch from clipboard

04 Sep 2019, 21:49

oh okay it worked, thank you so much.
is there any way that I can get each date and time as a separate variable?

I am trying to get the first and second date and time both separately as a variable.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: regexmatch from clipboard

04 Sep 2019, 21:53

They are separate variables. Each date is Date[1], Date[2], etc., and the corresponding times are Time[1], Time[2], etc. They were just shown next to each other in the MsgBox, but they are separate.

Do you mean you're rather have them as Date1, Date2, etc., and Time1, Time2, etc? There's really no advantage to that.
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

Re: regexmatch from clipboard

04 Sep 2019, 22:04

Yes, I will put them on gui interface one by one to the specific buttons with a better clickable copy button.

Oh perfect, yes I can call them as Date [1] ... Date [2] etc..
that is perfect, thank you a lot :)
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: regexmatch from clipboard

04 Sep 2019, 22:11

Glad to help.

Just note that there is no space in Date[1] (i.e. it's not Date [1] as you typed in your last message). Just trying to save you time hunting down a future problem.
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

Re: regexmatch from clipboard

05 Sep 2019, 12:08

Thank you so much Boiler, that was amazing,

I have one more question ;
for below condition now I am editing the regexmatch you provided but I couldn't be successful ;

clipboard is below;
there is tab before date .
between date and comes section there is a space and tab

Code: Select all

 
 
	Thu, Sep 5 	comes 11:00AM - 1:00PM	
	Thu, Sep 5 	comes 12:00PM - 2:00PM	
	Thu, Sep 5 	comes 1:00PM - 3:00PM	
	Thu, Sep 5 	comes 2:00PM - 4:00PM	

« Previous

;instead of 
FoundPos := RegExMatch(clip, "O)(\d{2}-\w{3}-\d{2}, \w+) (\d{2}:\d{2}\w{2} - \d{2}:\d{2}\w{2})", Match, StartPos)  
;I tried  
FoundPos := RegExMatch(clip, "O)(w{3},\ w{3} \d+) (\d{2}:\d{2}\w{2} - \d{2}:\d{2}\w{2})", Match, StartPos)
But that didn't work for this case, possibly I couldn't make that regex correct way. Could you please let me know how it should be for this case?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: regexmatch from clipboard

05 Sep 2019, 12:42

If I understood how you said it's formatted correctly, try this:

Code: Select all

FoundPos := RegExMatch(clip, "O)((\w{3}, \w{3} \d+)\s+comes (\d+:\d{2}\w{2} - \d+:\d{2}\w{2})", Match, StartPos)
It should match (for example): Thu, Sep 5 for date and 11:00AM - 1:00PM for time.
Yararli
Posts: 41
Joined: 18 May 2019, 20:26

Re: regexmatch from clipboard

05 Sep 2019, 13:05

Now I tried yours which looks great and thank you so much again,
and that works like charm
:dance:
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: regexmatch from clipboard

05 Sep 2019, 13:28

Just for fun:

Code: Select all

Clip =
(
Thu, Sep 5 comes 11:00AM - 1:00PM
abc
Thu, Sep 5 comes 12:00PM - 2:00PM
def
Thu, Sep 5 comes 1:00PM - 3:00PM
Thu, Sep 5 comes 2:00PM - 4:00PM
mno
Thu, Sep 5 comes 3:00PM - 5:00PM
)
dates := RegExReplace(Clip, "s).*?(\w+,\s\w+\s\d+\scomes\s(\d+:\d+\w+)\s-\s(?-1)(?(?=.*?(?-2))\R))|.*", "$1")
Date := []
Time := []
for k, v in StrSplit(dates, [" comes ", "`n"], "`r")
   ([Time, Date][mod(k, 2) + 1]).Push(v)

for k in Date
   TextOut .= Date[k] " >> " Time[k] "`n"
MsgBox, %TextOut%

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Lamron750, Rohwedder and 279 guests