Using a Regular Expression (RegEx) to Find a URL and not an email address

Find a URL and not an email addressA subscriber reached out to me (after trying for a few hours) for some help on creating a Regular Expression to pull-out URLs from text.  Specifically not including email addresses.  Since he gave it a solid try I wanted to help him out.  I did a google search and found this post which had the RegEx pattern for finding a URL and not an email address.  I then took the pattern and put it on Regular Expressions 101 to make sure it would work in AutoHotkey.   After that I adapted it to AutoHotkey code (see below) using RegExMatch.

Find a URL and not an email address

Code to Find a URL and not an email address

text=
(
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Lorem ipsum [email protected] dolor sit amet, (555)555.8957 the-Automator.com consectetuer adipiscing elit. 
https://www.linkedin.com/in/joeglines/ @recruiting  lorem ipsum
dolor sit http://github.com/maestrith/AHK-Studio elit.
dolor sit www.github.com/maestrith/AHK-Studio elit.
)
Pos:=1 ;set starting point

Loop, {
  ;Regex Pattern for URLs borrowed from https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url
  Found_Pos:=RegExMatch(Text,"i)((http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\[email protected]?^=%&\/~+#-])?)",Obj,Pos)
  Pos:=Found_Pos+(StrLen(obj)+1) ;Set next start position
  URLs.=obj "`r" ;add to emails variable
  If not OBJ ;If not found break Loop
    Break
}
MsgBox % URLs

 

Example Webservice / API call- Yelp and working through oAuth2

In our API webinar we discussed how oAuth2 works however there is no better way to explain it than to actually work through an example!  In the below video & code I demonstrate how to access Yelp to perform the “handshake” with oAuth1 and receive the oAuth2 authentication.  I also demonstrate using a named Regular Expression to isolate the returned token.

;~  https://www.yelp.com/developers/documentation/v3/authentication
ID:="94MAk4OrJa_y7ww5IV7HQg"
Secret:="eaHaNtMp4SbdBLB7aWpReLhe2oGWqeT32cMDG8ksMUc6r4ARA2RmPo7o6y6mG0pS"

EndPoint:="https://api.yelp.com/oauth2/token"
QueryString:=QueryString_Builder({"grant_type":"client_credentials","client_id":ID,"client_secret":Secret})
;***********API call to get Authorization Token*******************
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("POST",EndPoint . QueryString)
HTTP.Send()
data:=HTTP.ResponseText ;~ MsgBox,,title, % response
RegExMatch(data,".*access_token\x22:\s\x22(?<Token>.*?)\x22.*",Yelp_) ;RegEx to grab token
;~  MsgBox % Yelp_token

;~  https://www.yelp.com/developers/v3/manage_app?app_created=True
;~  https://www.yelp.com/developers/documentation/v3/business_search
EndPoint:="https://api.yelp.com/v3/businesses/search"

;~  https://www.yelp.com/developers/documentation/v2/all_category_list
;~  https://www.yelp.com/developers/documentation/v2/all_category_list/categories.json
QueryString:=QueryString_Builder({"term":"restaurants","location":"Coppell, TX","categories":"chicken_wings"})

;***********API call to yelp with Credentials*******************
HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("GET", EndPoint . queryString)
HTTP.SetRequestHeader("Authorization","Bearer " . Yelp_token)
HTTP.Send()
Response_Data:=HTTP.ResponseText ;~ MsgBox,,title, % response
Response_Data:=formatjson(Response_Data)
SciTE_Output(Response_Data) ;Text,Clear=1,LineBreak=1,Exit=0


;***********QueryString Builders*******************
QueryString_Builder(kvp){
for key, value in kvp
  queryString.=((A_Index="1")?(url "?"):("&")) key "=" value
return queryString
}

Video walking through the API example / oAuth2 process

 

 

 

Extract email address from text with Regular Expression in AutoHotkey

Extract email addressRegular Expressions are incredibly helpful for day-to-day tasks!  In this short video I demonstrate how easy it is to use a Regular Expression (RegEx) in AutoHotkey to extract email addresses from text.  The first half of the video shows how I use it to parse the haystack listed in the script.  The second half of the video I make a minor tweak and adapt it to work with any program that you can copy text to the clipboard.

Below is the code I demonstrate in the video

Haystack =
(
hello [email protected] more text
worked and [email protected]
did too and [email protected]
this also works [email protected]
432 432 [email protected] 43432
)
;Send ^c ;uncomment to use in any program
;Haystack:=Clipboard ;uncomment to use in any program

Pos:=1 ;set starting position
loop, {
Pos_Found:=RegExMatch(Haystack,"i)(?:[\w_\.-]+)@(?:[\w\.-]+)\.(?:[a-z\.]{2,6})",mail,Pos)
Pos:=Pos_Found+StrLen(mail)+1 ;move location to new position + mail lenght+ 1
IfEqual,mail,,break ;Break loop if no mail found
mails.=mail "`n"
}
MsgBox % mails

 

Extract email addresses via Regular Expression in AutoHotkey