RegEx 105- Simplify RegEx with escapes in AutoHotkey Regular Expressions

Woman holding a green-and-white Schüttelbecher-style bottle outdoors, with a label reading Schüttelbecher on the front Wait, let me correct that toescapes in AutoHotkey Regular ExpressionsUsing RegEx to identify text patterns is an awesome skill-set to have.  In the below tutorial we review some of the shortcuts /escapes in AutoHotkey Regular Expressions.  These can make your code much more reader-friendly and easier to maintain.

Here’s a cheat-sheet for some of the more popular escapes:

  • \w =word character
  • \d = digit
  • \s =white space (tab, space, etc)
  • \b = word boundary

Remember, for each of the above, you can select the inverse of them simply by capitalizing the letter.

Demo video showing how to use Shortcuts / Escapes in AutoHotkey Regular Expressions

AutoHotkey Merchandise-White Stress ball

Using the Autohotkey StringSplit / StrSplit() function to slice strings

autohotkey StringSplitAutoHotkey Merchandise-White Stress ballUsing the AutoHotkey StringSplit / StrSplit() function

AutoHotkey has some pretty cool functions for slicing strings. Not quite as robust as Python however they definitely cover the majority of needs with ease.  In the below video I demonstrate some simple usage if it as well as a way to access the Array it creates without ever saving it to a variable!  :)

Tutorial demonstrating AutoHotkey StringSplit function

Great AutoHotkey script to URL Encode/Decode and parse URL parameters

parse url parameters

Working in email marketing and Website design I frequently need to URL Encode / Decode and parse URL parameters.  This short AutoHotkey script makes doing so a breeze!  I watch my colleagues struggle through trying to read encoded URLs or try and find every parameters in a given URL.  With this script you can see just how easy it is to decode a URL and then parse it on the “?” then by every “&” putting them on new lines.

A second usage is to help examine how websites use URL Parameters to serve-up a webpage.  This can be very helpful when doing Web Scraping as you can automate generating the page from a given website.

Video demonstrating how to Parse URL Parameters

AutoHotkey script to Parse URL Parameters

 
#SingleInstance, Force
;***********Encode URL******************* 
!e:: ; alt+e encodes highlighted text
gosub Store_Clipboard_Copy_Selected_Text ;backup original clipboard
Clipboard:=UriEncode(clipboard)  ;convert to URL encoded
Gosub Paste_and_Restore_Stored_Clipboard ;restore clipboard
return
;***********Decode URL******************* 
!d:: ;Alt+d will Decode highlighted text
gosub Store_Clipboard_Copy_Selected_Text
Clipboard:=URiDecode(clipboard) ;Decode URL
Gosub Paste_and_Restore_Stored_Clipboard ;restore clipboard
return
!w:: ;Decode URL and Wrap on parameters
gosub Store_Clipboard_Copy_Selected_Text
clipboard:=URiDecode(clipboard) 
StringReplace,clipboard,clipboard,?,`r`n`t?,All ;Line break and tab indent <strong>parse URL parameters</strong>.
StringReplace,clipboard,clipboard,&,`r`n`t`t&,All ;Line break and double tab indent
Gosub Paste_and_Restore_Stored_Clipboard
return
;****************************** 
;*******Store Clipboard- save for restoring, and copy selected text to clipboard****************
Store_Clipboard_Copy_Selected_Text:
Store:=ClipboardAll  ;Store full version of Clipboard
  clipboard = ; Empty the clipboard
  SendInput, ^c ;changd from Send  11/23
  ClipWait, 1
    If ErrorLevel ;Added errorLevel checking
      {
        MsgBox, No text was sent to clipboard
        Return
      }
return
;**********************restore clipboard*********************************
Paste_and_Restore_Stored_Clipboard:  ;put back original content
SendEvent , ^v
Clipboard:=Store
return

;***********https://autohotkey.com/board/topic/17367-url-encoding-and-decoding-of-special-characters/******************* 
uriDecode(str) {
    Loop
	If RegExMatch(str, "i)(?<=%)[\da-f]{1,2}", hex)
	   StringReplace, str, str, `%%hex%, % Chr("0x" . hex), All
	   Else Break
	Return, str
}
UriEncode(Uri, RE="[0-9A-Za-z]"){
    VarSetCapacity(Var,StrPut(Uri,"UTF-8"),0),StrPut(Uri,&Var,"UTF-8")
    While Code:=NumGet(Var,A_Index-1,"UChar")
    Res.=(Chr:=Chr(Code))~=RE?Chr:Format("%{:02X}",Code)
    Return,Res  
}

If this interested you, you might also want to investigate using web scraping with AHK.