Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
boiler
Posts: 16903
Joined: 21 Dec 2014, 02:44

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?  Topic is solved

27 Jul 2021, 05:52

I think this should take care of it. If the clipboard doesn't include the strings google.com or fbcdn.net, it should not touch it.

Code: Select all

CleanGoogleLink() {
	if !(InStr(Clipboard, "google.com") || InStr(Clipboard, "fbcdn.net"))
		return
	OnClipboardChange("CleanGoogleLink", 0)
	Clipboard := RegExReplace(Clipboard, "google.com.*\K&oq=.*")
	Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
	Clipboard := StrReplace(Clipboard, "&source=lnt")
	Clipboard := RegExReplace(Clipboard, "fbcdn.net.*\K&bytestart=.*")
	OnClipboardChange("CleanGoogleLink", 1)
}
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

27 Jul 2021, 07:47

this does resolve the issue, but is it possible to

1. insert wildcards as such?

Code: Select all

if !(InStr(Clipboard, "*.com/*") || (InStr(Clipboard, "*.net/*")
2. Alternatively, can I specify only "text within clipboard" to be processed by this script?
User avatar
boiler
Posts: 16903
Joined: 21 Dec 2014, 02:44

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

27 Jul 2021, 07:55

You don’t need wildcards because that’s how InStr() would behave already. Think about it: It’s checking if those things are in the string, no matter what’s around them. So it can just be like this:

Code: Select all

if !(InStr(Clipboard, ".com/") || (InStr(Clipboard, ".net/"))
(you left off the last ), btw)


There is a way for it to act only if the contents of the clipboard is text, but when copying files, they are also seen as text in the clipboard, so you would have the same problem as before when copying files.
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

29 Jul 2021, 03:50

now this relates to my earlier question about covering all sorts of domains within 4 characters. How do I code the InStr part that also covers these urls?
User avatar
boiler
Posts: 16903
Joined: 21 Dec 2014, 02:44

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

29 Jul 2021, 05:40

You can't use InStr for that, you would use RegExMatch:

Code: Select all

if !RegExMatch(Clipboard, "\.[a-z]{2,4}")
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

29 Jul 2021, 07:18

didn't know I could use regexmatch along with the instr command too. works great, thanks!
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

29 Jul 2021, 20:48

boiler wrote:
27 Jul 2021, 05:52
I think this should take care of it. If the clipboard doesn't include the strings google.com or fbcdn.net, it should not touch it.

ah.. the problem is coming back now again, but this time it's intermittent. Comes back once every 30mins, and I have to reload the script in order for it not to interrupt my cut/copy/pasting of files. any idea why?
User avatar
boiler
Posts: 16903
Joined: 21 Dec 2014, 02:44

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

29 Jul 2021, 21:28

I believe I see why, but I'd be surprised if it's really intermittent. It would seem that any file with an extension would fit that criteria because the end of a URL such as .com and the extension of a file like .ahk would both be seen as the same by that RegEx pattern. Adding a slash should differentiate between the two:

Code: Select all

if !RegExMatch(Clipboard, "\.[a-z]{2,4}/")
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

29 Jul 2021, 22:06

I see, then that wouldn't cover top-level domains without a forward slash such as www.abc.com

let me think if there's a better way to do this.. thanks!
User avatar
boiler
Posts: 16903
Joined: 21 Dec 2014, 02:44

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

30 Jul 2021, 00:09

Why do you need it to cover cases where it’s not followed by a forward slash? Those cases have nothing to be cleaned, which is the purpose of the function, so returning without doing anything would be fine.
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

30 Jul 2021, 21:23

oh yes you're right.. I confused myself with another script that I was doing. Thanks a lot!

on the same note, is it possible to make the same script work without checking clipboard at all?

E.g. when I click on any link that matches the above patterns, "abc.com/&oq=abc" it will visit abc.com instead. This doesn't involve clipboard, so I was thinking how the script will look like instead.
User avatar
boiler
Posts: 16903
Joined: 21 Dec 2014, 02:44

Re: Regex to remove everything that comes after &oq=, only IF domain is google.com when found in clipboard?

30 Jul 2021, 21:54

If there is a way, I don’t know how to do it. It might be able to be done by injecting code into the default browser’s process. That’s beyond the scope of writing AHK scripts, though.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 108 guests