Actually remove needle from haystack? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Actually remove needle from haystack?

17 Oct 2018, 14:43

Hi Folks,
If I run this code

Code: Select all

#SingleInstance force
  ;#################################
HaystackText := "The red firetruck."  
Needle := "(fire|water)"
RegExMatch(HaystackText, Needle, myVar) 
MsgBox Found %myVar% `nHaystack is %HaystackText%
return
The result is:
---------------------------
Found fire
Haystack is The red firetruck.
---------------------------

What would cause it to result in this:
---------------------------
Found fire
Haystack is The red truck.
---------------------------
?.
I'll explain more with a reply too.

(Side comment: Syntax highlighting not working in forum ?)
ste(phen|ve) kunkel
User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Re: Actually remove needle from haystack?

17 Oct 2018, 14:53

I'll paste my actual code below.
It's a script that creates an Outlook Calendar item. It works like:
1. Copy selected text
2. Regex match possible location, time, date, then subject.
2b. Pop up a Msg of the haystack and matches.
3. Open the Calendar form
4. Use Outlook's hotkeys to enter the things.

If I use if on this text:
Time correction: 12:00 NOON Matt Smith. 1st floor conf.

Then I get this:
---------------------------
Clipboard is
===================
[Time coection: 12:00 NOON Matt Smith. floor conf.]
===================

date is [ 1st]
time is [12:00 ]
location is [rr]
Subject is [Time]
---------------------------

Notice that the "rr" in correction is matched as a location, but it is also removed from the haystack text. "1st" is also removed, but "12:00" is not. I'd like to know why it does this, so I can do it on purpose.

Here is the entire script (sorry about lack of syntax highlighting)

Code: Select all

#NoEnv
#SingleInstance force
;#################################

;Menu, Tray, Icon, %A_ScriptDir%\magnet-arrow-fugueIcon16.ico
AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.
ClipboardOld = %ClipboardAll%
Clipboard =  ; Must start off blank for detection to work.
Send ^c
ClipWait, 1
if ErrorLevel  ; ClipWait timed out.
  {
    MsgBox, No text selected(?)`n`n   (Script exiting.)
    ExitApp
  }

;######## GET LOCATION #####################
reLocation := "\b[a-z]{2,}'s\s(office|room|classroom|rm)|((Resource\s)(Room|Rm\s)|RR)|(room|rm)\s(A|B|C|D|\d{3})|(Counseling)|(Office)\b"  
reLocPre := "in\s(the\s)?"  ; don't capture

RegExMatch(Clipboard, "i)" reLocPre "(\K)" reLocation, myLocation)
 if (!myLocation)
    {
    RegExMatch(Clipboard, reLocation, myLocation) 
    }

;######## GET TIME #########################
reTime := "\b(([0-2]?[0-9]:[0-5])[0-9](\s?(pm|am|p|a)?)|[12]?[0-9](pm|am|p|a))|(before\s|after\s)school|noon|in the morning\b"

RegExMatch(Clipboard, "i)" reTime, myTime)
  If (myTime = "before school" )||(myTime = "in the morning")
  {
    myTime := "7:30 am"
  }
  Else If (myTime = "after school")
  {
    myTime := "2:45 pm"
  }
  
;######### GET DATE ######################## 
re2WeeksPre := "(((2|two)\sweeks\s)(from\s)?|((week\s)?after next\s(on\s)?))"   ; don't capture  
re2WeeksSuf := "(((in\s)?(2|two) weeks)|((week )?after next))"                  ; don't capture
re1WeekPre := "(next\s(week\s)?)|(week\s(from|after)\s)"                        ; don't capture
re1WeekSuf := "(next( week)?)"                                                  ; don't capture

reNumDate := "\b(([0-9]|10|11|12)[-./]([0-2]?[0-9])[-./](20)?[0-9][0-9])"
reWDay := "\b(((Mon|Tue(s)?|Wed(nes|s)?|Thu(rs|r)?|Fri|Sat(ur)?|Sun))(day|d)?|(day\safter\s)?tomorrow)\b"
reMonth := "\b((Jan|Feb)(ru(ary)?)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|(Sep(tem)?|Oct(o)?|Nov|Dec)(em)?(ber)?)\b"
re1st31st := "\b(?<!:)([0-2]?[0-9]|30|31)(st|nd|rd|th)?(?!:)\b"

RegExMatch(Clipboard, reNumDate, myDate) ; e.g. 01-01-2016
  if (!myDate)
    {
    RegExMatch(Clipboard, "i)" reMonth "(\s)" re1st31st "|" re1st31st "(\sof\s)" reMonth, myDate) ; e.g. Jan 1st | 1st of Jan
      if (!myDate)
        {
        RegExMatch(Clipboard, "i)" re2WeeksPre "\K" reWDay "|"  reWDay "(?=\s" re2WeeksSuf ")", myDate) ; e.g. Mon in two weeks
        myDatePrefix := "2 weeks " 
          if (!myDate)
            {
            RegExMatch(Clipboard, "i)(" re1WeekPre ")\K" reWDay "|"  reWDay "(?=\s" re1WeekSuf ")", myDate)    ; e.g. next Mon
            myDatePrefix := "next " 
              if (!myDate)
                {
                RegExMatch(Clipboard, "i)" reWDay, myDate) ; e.g. Monday
                myDatePrefix =
                  if (!myDate)
                   ; MsgBox ReWday = %reWDay%   myDate = %myDate%
                    {
                    RegExMatch(Clipboard, "i)" reMonth, myDateM)  ; e.g. (January)(1st)
                    RegExMatch(Clipboard, re1st31st, myDateD) 
                    myDate = %myDateM% %myDateD%
                    }
                }
            }
        }
    }
  ;sleep, 500
myDate = %myDatePrefix%%myDate%

;#########GET SUBJECT#########################
StringReplace, clipboard, clipboard, %myLocation%, , All    ; removes myLocation so that it's not mistaken as the Subject.
StringReplace, clipboard, clipboard, %myDate%, , All        ; removes myDate
reSubject := "\b[A-Z][a-z]*(\s[A-Z][a-z]*)?\b"
reSubjPre := "meeting\sfor"     ; don't capture
reSubjSuf := "'s"               ; don't capture

RegExMatch(Clipboard, "i)" reSubjPre "(\K\s)" reSubject, mySubject)
 if (!mySubject)
    {
    RegExMatch(Clipboard, reSubject "(?=\s" reSubjSuf ")", mySubject) 
      if (!mySubject)
        {
        RegExMatch(Clipboard, reSubject, mySubject) 
        }
    }

;#############################################
MsgBox Clipboard is`n===================`n [%Clipboard%]`n===================`n`ndate is [%myDate%]`ntime is [%myTime%]`nlocation is [%myLocation%]`n Subject is [%mySubject%]
;MsgBox date is [%myDate%]`ntime is [%myTime%]`nlocation is [%myLocation%]`n Subject is [%mySubject%]

run, C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE /c ipm.appointment
WinWaitActive, Untitled - Appointment, , 15

	SendInput, !u%mySubject% ; Subject
	SendInput, !t%myDate%
	SendInput, !t{Tab}{Tab}%myTime%
	SendInput, !i%myLocation%


Clipboard = %ClipboardOld%  ; Restore previous contents of clipboard. 
ExitApp

Esc:: 
;SoundPlay *-1
Clipboard = %ClipboardOld%  ; Restore previous contents of clipboard. 
ExitApp

/*
TEST STRINGS
There is a meeting for Jimmy Bob after school next Wed in the Resource Room.
Let's do Monday in 2 weeks in Rm 101.
The transition meeting for Jenny is at noon in the A Commons.
Nov 17 before school for Billy's thing.
Day after tomorrow in the morning.


Navigation of Outlook Calendar Item Form:   (updated 10-10-2018)
/c ipm.appointment
Subject !u
Location !i
Start date !t
Start time !t{Tab}{Tab}
End Date !d
End time !d{Tab}{Tab}
Notes !d{Tab}{Tab}{Tab}{Tab}  Developer Tab must be hidden.
Save and Close !s
*/
EDIT: Screenshot
Image
ste(phen|ve) kunkel
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Actually remove needle from haystack?  Topic is solved

17 Oct 2018, 19:27

kunkel321 wrote:The result is:
---------------------------
Found fire
Haystack is The red firetruck.
---------------------------

What would cause it to result in this:
---------------------------
Found fire
Haystack is The red truck.
---------------------------

;) :arrow: Regexreplace()

Code: Select all

#SingleInstance force

HaystackText := "The red firetruck."  
Needle := "(fire|water)"
if RegExMatch(HaystackText, Needle, myVar) 
msgbox, % "found " . myvar . "`nHaystack is " . HaystackText := regexreplace(HaystackText, Needle)      ;  remove "fire" or "water" from HaystackText
return
Cheers,
User avatar
kunkel321
Posts: 1042
Joined: 30 Nov 2015, 21:19

Re: Actually remove needle from haystack?

18 Oct 2018, 09:08

Ha! I'm so stupid. I intentionally built that functionality into the script already and forgot. Before posting, I searched my script for "regexreplace" and didn't find it, but it's actually "stringreplace" that I used. (Long script, second post, 2/3s the way down.) Sorry about that everyone. And thanks SpeedMaster!
ste(phen|ve) kunkel

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Noitalommi_2, ulysim and 301 guests