I've extracted the domain using brute force rather than finesse, but that should be more reliable.
This job is a little easier with AppleScript:
set theURL to "https://forum.keyboardmaestro.com/t/find-matching-text-from-clipboard-in-a-list-of-possibilities/23768"
set myDomainList to paragraphs 2 thru -2 of "
apple
example
ibm
keyboardmaestro
"
set theURL to theURL as URL
set webHost to DNS form of host of theURL
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
set webDomain to text item -2 of webHost
set AppleScript's text item delimiters to oldTIDS
if webDomain is in myDomainList then
return true
end if
Wow, thanks! And now I don't feel so badly for not finding the solution. It'd be great if KM just natively supported a function like this, where you can easily check if a value (possibly regex manipulated) is contained in a list.
And because this was code I was running before I started filtering, in order to exit the macro quickly if there's not a match, I think I'll have to skip implementing this. The current solution I have works, but suffers a bit in readability. (I store the list of hosts as a regex, i.e. apple|ibm|example, and then just do a search on clipboard using regex, with the regex set to the variable.
I think I can probably add a single step, though, and restore readability: Store the list as above, one per line, then just replace line breaks to pipes via search before passing to the search.
Again, thanks for the effort - no way I would've gotten that on my own!
While I used the %FrontBrowserURL% token instead of a URL on the clipboard to expedite testing, as far as I can tell, there's no reason in theory that it shouldn't work with that as well.
Thanks for another solution, @gglick! After mulling it over, though, I realized I was overcomplicating it: My objective was to exit my macro as quickly as possible if it didn't contain a URL I wanted to filter—because it runs on clipboard change, so I didn't want to linger in code if nothing was going to happen.
I also wanted a readable list of hosts, which is what started me on the path that led to this question. But I realized I could have both with a little help from find and replace:
The list form lets me easily see and edit, then I use a search and replace to swap the line breaks for regex pipes (the 'or' symbol), and then I run a regex search looking for matches in the clipboard's text. I have the search set to end the macro on failure (without notification).
This is working perfectly for me, but I wouldn't have gotten there without everyone's help here, so thanks again!