I feel like the answer is something like, “Cut the URL at the question mark, and keep the first part”, but I couldn’t figure that out.
Alternatively I played around with “Change the question mark to a space (which creates two ‘words’) and then keep only the first part”. But couldn’t make that work either. I tried a couple regex things but kept only getting “http”.
Any help someone can provide here would be much appreciated! Thanks.
Probably you went wrong because you were fooling with "?" which is a reserved character in regex.
^(https?://[^?]+)\?
^ == Beginning of line
( == Start of Capture
https == Literal String
? == Zero or 1 of the preceding character
:// == Literal String
[^?] == Range - Any character NOT '?'
+ == One or more
) == End of Capture
\? == Literal Question mark - made by escaping the ? with a backslash.
A simpler way of doing this if you need only the text before the '?' is to find/replace.
In your case I'd probably capture the url to a variable and then search/replace in the variable.
Find:
\?.+
Replace:
Nothing.
Of course this little job is easy to do with AppleScript too:
set AppleScript's text item delimiters to "?"
tell application "Keyboard Maestro Engine"
set myURL to the clipboard
if myURL starts with "http" and myURL contains "?" then
set urlFront to text item 1 of myURL
try
set value of variable "myVar" to urlFront
on error
make new variable with properties {name:"myVar", value:urlFront}
end try
else
error "Malformed string in clipboard!"
end if
end tell
Thanks Tim for posting the question (I won’t use it as much as you, but I do manually strip the cruft off URLs when I paste them, and I’d never thought of using KM for this) and Chris for posting the how to (I really need to learn regex).
I’ve made a slightly modified version, more suitable for casual use. It’s simply a variation on the standard paste shortcut, which strips the cruft from a full URL already copied to the clipboard when pasted.