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
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

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

Post by milkygirl90 » 21 Jul 2021, 20:03

Code: Select all

https://www.google.com/search?q=test&sxsrf=ALeKk03QE1vFciWFYpcAzoAO_lbPW_Da-w%3A1626915750333&ei=psP4YL-xE8b69QOPh6_gCA&oq=test&gs_lcp=Cgdnd3Mtd2l6EAMyCggAELEDEIMBEEMyBQgAELEDMgUIABCxAzIFCAAQsQMyCAgAELEDEIMBMggIABCxAxCDATIFCAAQsQMyCAgAELEDEIMBMgUIABCxAzIICAAQsQMQgwE6BAgjECc6BQgAEJECOgcIABCxAxBDOgQIABBDOgsIABCxAxCDARCRAjoECC4QQzoNCC4QsQMQxwEQowIQQzoKCC4QxwEQowIQQzoCCABKBAhBGABQuu8BWM3xAWDc9AFoAHACeACAATqIAfABkgEBNZgBAKABAaoBB2d3cy13aXrAAQE&sclient=gws-wiz&ved=0ahUKEwi_nIK7vfXxAhVGfX0KHY_DC4wQ4dUDCA4&uact=5
sofista
Posts: 644
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

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

Post by sofista » 21 Jul 2021, 20:30

Hi, it could be as follows (tested)

Code: Select all

Clipboard:="https://www.google.com/search?q=test&sxsrf=ALeKk03QE1vFciWFYpcAzoAO_lbPW_Da-w%3A1626915750333&ei=psP4YL-xE8b69QOPh6_gCA&oq=test&gs_lcp=Cgdnd3Mtd2l6EAMyCggAELEDEIMBEEMyBQgAELEDMgUIABCxAzIFCAAQsQMyCAgAELEDEIMBMggIABCxAxCDATIFCAAQsQMyCAgAELEDEIMBMgUIABCxAzIICAAQsQMQgwE6BAgjECc6BQgAEJECOgcIABCxAxBDOgQIABBDOgsIABCxAxCDARCRAjoECC4QQzoNCC4QsQMQxwEQowIQQzoKCC4QxwEQowIQQzoCCABKBAhBGABQuu8BWM3xAWDc9AFoAHACeACAATqIAfABkgEBNZgBAKABAaoBB2d3cy13aXrAAQE&sclient=gws-wiz&ved=0ahUKEwi_nIK7vfXxAhVGfX0KHY_DC4wQ4dUDCA4&uact=5"

RegExMatch(Clipboard, ".*google.com.*(?=&oq=)", m)
MsgBox, % m
return

;Output:

;https://www.google.com/search?q=test&sxsrf=ALeKk03QE1vFciWFYpcAzoAO_lbPW_Da-w%3A1626915750333&ei=psP4YL-xE8b69QOPh6_gCA

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?

Post by milkygirl90 » 21 Jul 2021, 21:01

Is it possible to monitor clipboard for links that start with google.com without using loop command?

used this but it didn't work;

Code: Select all

#IfWinActive ahk_group MyBrowsers ;Generic Browser hotkeys
RegExMatch(Clipboard, ".*google.com.*(?=&oq=)", m)
Clipboard := m
Send ^v
return
User avatar
boiler
Posts: 16705
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?

Post by boiler » 21 Jul 2021, 21:39

#If directives only affect whether hotkeys are active, not other code. You can use OnClipboardChange() to have a function check the contents of the clipboard every time it changes.
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?

Post by milkygirl90 » 22 Jul 2021, 00:17

boiler wrote:
21 Jul 2021, 21:39
#If directives only affect whether hotkeys are active, not other code. You can use OnClipboardChange() to have a function check the contents of the clipboard every time it changes.
Thanks for reminding! Managed to fix it as follows:

Image

However I noticed that pausing the script / suspend hotkeys still keeps the script active (hence I couldn't paste the code above). Any idea how I can temporarily disable it?

On the same note, is there a global shortcut to disable ALL AutoHotkey scripts concurrently too?
User avatar
boiler
Posts: 16705
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?

Post by boiler » 22 Jul 2021, 04:44

milkygirl90 wrote: However I noticed that pausing the script / suspend hotkeys still keeps the script active (hence I couldn't paste the code above). Any idea how I can temporarily disable it?
Do you mean you want to disable the same script that you are about to edit? Just exit the script. You have to restart it or reload it anyway for the new changes to take effect. It’s not like you can just edit a script and keep it running and have it start using the new changes.

milkygirl90 wrote: On the same note, is there a global shortcut to disable ALL AutoHotkey scripts concurrently too?
Not that I know of. You could write a script to do something like that. It would likely need to communicate with each of the scripts to have them pause/suspend themselves.
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?

Post by milkygirl90 » 22 Jul 2021, 06:22

I don't mean disabling the script while editing it. I mean simply temporarily disabling it.. which didn't work when I tried pausing (only exit works).
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

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

Post by braunbaer » 22 Jul 2021, 06:54

Suspending a script disables the hotkeys and hotstrings. It does not disable event handlers like OnClipboardchange.

But in the eventhandler, you can check the variable A_IsSuspended and exit the routine if A_IsSuspended=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?

Post by milkygirl90 » 22 Jul 2021, 21:52

braunbaer wrote:
22 Jul 2021, 06:54
Suspending a script disables the hotkeys and hotstrings. It does not disable event handlers like OnClipboardchange.

But in the eventhandler, you can check the variable A_IsSuspended and exit the routine if A_IsSuspended=1
now I get it. 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?

Post by milkygirl90 » 25 Jul 2021, 19:05

milkygirl90 wrote:
21 Jul 2021, 20:03

Code: Select all

https://www.google.com/search?q=test&sxsrf=ALeKk03QE1vFciWFYpcAzoAO_lbPW_Da-w%3A1626915750333&ei=psP4YL-xE8b69QOPh6_gCA&oq=test&gs_lcp=Cgdnd3Mtd2l6EAMyCggAELEDEIMBEEMyBQgAELEDMgUIABCxAzIFCAAQsQMyCAgAELEDEIMBMggIABCxAxCDATIFCAAQsQMyCAgAELEDEIMBMgUIABCxAzIICAAQsQMQgwE6BAgjECc6BQgAEJECOgcIABCxAxBDOgQIABBDOgsIABCxAxCDARCRAjoECC4QQzoNCC4QsQMQxwEQowIQQzoKCC4QxwEQowIQQzoCCABKBAhBGABQuu8BWM3xAWDc9AFoAHACeACAATqIAfABkgEBNZgBAKABAaoBB2d3cy13aXrAAQE&sclient=gws-wiz&ved=0ahUKEwi_nIK7vfXxAhVGfX0KHY_DC4wQ4dUDCA4&uact=5
coming back to the same question, how do I:

1. replace my search term with %s in the same URL while still removing what comes after &oq=?

Code: Select all

https://www.google.com/search?q=fibonacci+ratio+in+nature&num=20&safe=off&hl=en&source=lnt&tbs=qdr:m3&sa=X&ei=pB9_UceaE4f3rQe_uIGIBw&ved=0CB0QpwUoBA&biw=2560&bih=1315&oq=test&gs_lcp=Cgdnd3Mtd2l6EAMyCggAELEDEI

https://www.google.com/search?q=%s&num=20&safe=off&hl=en&source=lnt&tbs=qdr:m3&sa=X&ei=pB9_UceaE4f3rQe_uIGIBw&ved=0CB0QpwUoBA&biw=2560&bih=1315
2. How to remove &source=lnt within the search term in the same code?

3. Similarly in the same code, How do I remove everything that comes after &oq= AND &sa=?

4. Lastly, what is wrong with my syntax below for Else if, and why does it not clean my FB link?

Code: Select all

CleanGoogleLink() {
	if RegExMatch(Clipboard, ".*google.com.*(?=&oq=)", m) {
						
				else if RegExMatch(Clipboard, ".*fbcdn.net*(?=&bytestart=)", m)
				OnClipboardChange("CleanGoogleLink", 0)
		Clipboard := m
		OnClipboardChange("CleanGoogleLink", 1)
	}
}

Code: Select all

Input: 
https://video.fsin2-1.fna.fbcdn.net/v/t42.9040-2/10073700_631492016194_9094004499900574657_n.mp4?_nc_cat=100&ccb=1-3&_nc_sid=5aebc0&efg=eyJ2ZW5jb2RlX3RhZyI6ImRhc2hfdjRfcGFzc3Rocm91Z2hfZnJhZ18yX3ZpZGVvIn0%3D&_nc_ohc=tnHZ-LAVgKcAX_2xb1A&_nc_ht=video.fsin2-1.fna&oh=3ba505363cd8595ab2a5d02c3d30e109&oe=60FE4EE4&bytestart=360934474&byteend=364584420

Output
https://video.fsin2-1.fna.fbcdn.net/v/t42.9040-2/10073700_631492016194_9094004499900574657_n.mp4?_nc_cat=100&ccb=1-3&_nc_sid=5aebc0&efg=eyJ2ZW5jb2RlX3RhZyI6ImRhc2hfdjRfcGFzc3Rocm91Z2hfZnJhZ18yX3ZpZGVvIn0%3D&_nc_ohc=tnHZ-LAVgKcAX_2xb1A&_nc_ht=video.fsin2-1.fna&oh=3ba505363cd8595ab2a5d02c3d30e109&oe=60FE4EE4
Last edited by milkygirl90 on 26 Jul 2021, 17:38, edited 1 time in total.
User avatar
boiler
Posts: 16705
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?

Post by boiler » 26 Jul 2021, 04:36

milkygirl90 wrote: 4. Lastly, what is wrong with my syntax below for Else if, and why does it not clean my FB link?

Code: Select all

CleanGoogleLink() {
	if RegExMatch(Clipboard, ".*google.com.*(?=&oq=)", m) {
						
				else if RegExMatch(Clipboard, ".*fbcdn.net*(?=&bytestart=)", m)
				OnClipboardChange("CleanGoogleLink", 0)
		Clipboard := m
		OnClipboardChange("CleanGoogleLink", 1)
	}
}
If you follow the logic of the above code, when the if condition is false, it wouldn’t drop down to the next line inside the braces, it would jump to after the closing brace, which is right before the end of the function. That's where an else associated with that if would have to go. So inside the braces, you just start with an else, that had no if associated with it. What you posted wouldn’t even run because of your syntax error (an else that doesn’t match an if), let alone not clean your FB link. The following would do what you want:

Code: Select all

CleanGoogleLink() {
	OnClipboardChange("CleanGoogleLink", 0)
	if RegExMatch(Clipboard, ".*google.com.*(?=&oq=)", m)
		Clipboard := m
	else if RegExMatch(Clipboard, ".*fbcdn.net.*(?=&bytestart=)", m) ; you were missing a "." before the second "*"
		Clipboard := m
	OnClipboardChange("CleanGoogleLink", 1)
}

I would do this instead:

Code: Select all

CleanGoogleLink() {
	OnClipboardChange("CleanGoogleLink", 0)
	Clipboard := RegExReplace(Clipboard, "google.com.*\K&oq=.*")
	Clipboard := RegExReplace(Clipboard, "fbcdn.net.*\K&bytestart=.*")
	OnClipboardChange("CleanGoogleLink", 1)
}
Not sure I understand your other questions.
User avatar
boiler
Posts: 16705
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?

Post by boiler » 26 Jul 2021, 04:47

(I edited the above post to fix an issue with your FB RegEx (see comment in the code) and added a RegExReplace version.)
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?

Post by milkygirl90 » 26 Jul 2021, 04:57

ah I see. Thanks for pointing that out. I amended it to exactly what you wrote above, but it still doesn't clean up my link below though:

Code: Select all

https://video.fsin2-1.fna.fbcdn.net/v/t42.9040-2/10073700_631492016194_9094004499900574657_n.mp4?_nc_cat=100&ccb=1-3&_nc_sid=5aebc0&efg=eyJ2ZW5jb2RlX3RhZyI6ImRhc2hfdjRfcGFzc3Rocm91Z2hfZnJhZ18yX3ZpZGVvIn0%3D&_nc_ohc=tnHZ-LAVgKcAX_2xb1A&_nc_ht=video.fsin2-1.fna&oh=3ba505363cd8595ab2a5d02c3d30e109&oe=60FE4EE4&bytestart=360934474&byteend=364584420
User avatar
boiler
Posts: 16705
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?

Post by boiler » 26 Jul 2021, 05:00

Are you sure you implemented it correctly? This shows it works:

Code: Select all

Clipboard := "https://video.fsin2-1.fna.fbcdn.net/v/t42.9040-2/10073700_631492016194_9094004499900574657_n.mp4?_nc_cat=100&ccb=1-3&_nc_sid=5aebc0&efg=eyJ2ZW5jb2RlX3RhZyI6ImRhc2hfdjRfcGFzc3Rocm91Z2hfZnJhZ18yX3ZpZGVvIn0%3D&_nc_ohc=tnHZ-LAVgKcAX_2xb1A&_nc_ht=video.fsin2-1.fna&oh=3ba505363cd8595ab2a5d02c3d30e109&oe=60FE4EE4&bytestart=360934474&byteend=364584420"
CleanGoogleLink()
MsgBox, % Clipboard

CleanGoogleLink() {
	OnClipboardChange("CleanGoogleLink", 0)
	Clipboard := RegExReplace(Clipboard, "google.com.*\K&oq=.*")
	Clipboard := RegExReplace(Clipboard, "fbcdn.net.*\K&bytestart=.*")
	OnClipboardChange("CleanGoogleLink", 1)
}

Also works if you use it as intended by copying to the clipboard:

Code: Select all

#Persistent
OnClipboardChange("CleanGoogleLink")
return

CleanGoogleLink() {
	OnClipboardChange("CleanGoogleLink", 0)
	Clipboard := RegExReplace(Clipboard, "google.com.*\K&oq=.*")
	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?

Post by milkygirl90 » 26 Jul 2021, 17:29

I noticed u included a \K in the code above and it worked. I read the regex documentation but didn't find anything related to \K. do you mind sharing how it works? https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm#Common

There's also a few questions earlier that I'm still not sure how to use regex to solve.. hope you don't mind helping me.. thank you so much 🙏🏼

1. replace search term with %s in the same URL while still removing what comes after &oq=?

Code: Select all

https://www.google.com/search?q=fibonacci+ratio+in+nature&num=20&safe=off&hl=en&source=lnt&tbs=qdr:m3&sa=X&ei=pB9_UceaE4f3rQe_uIGIBw&ved=0CB0QpwUoBA&biw=2560&bih=1315

https://www.google.com/search?q=%s&num=20&safe=off&hl=en&source=lnt&tbs=qdr:m3&sa=X&ei=pB9_UceaE4f3rQe_uIGIBw&ved=0CB0QpwUoBA&biw=2560&bih=1315
2. How to remove &source=lnt within the search term in the same code?
User avatar
boiler
Posts: 16705
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?

Post by boiler » 26 Jul 2021, 18:20

milkygirl90 wrote: I noticed u included a \K in the code above and it worked. I read the regex documentation but didn't find anything related to \K. do you mind sharing how it works? https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm#Common
The \K is explained in the page you linked. Do a search on that page for it if you can't find it. See if that explains it well enough for you.

milkygirl90 wrote: 1. replace search term with %s in the same URL while still removing what comes after &oq=?
Like this:

Code: Select all

#Persistent
OnClipboardChange("CleanGoogleLink")
return

CleanGoogleLink() {
	OnClipboardChange("CleanGoogleLink", 0)
	Clipboard := RegExReplace(Clipboard, "google.com.*\K
	Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
	Clipboard := RegExReplace(Clipboard, "fbcdn.net.*\K
	OnClipboardChange("CleanGoogleLink", 1)
}

milkygirl90 wrote: 2. How to remove &source=lnt within the search term in the same code?
Use a simple StrReplace() for this:

Code: Select all

#Persistent
OnClipboardChange("CleanGoogleLink")
return

CleanGoogleLink() {
	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?

Post by milkygirl90 » 26 Jul 2021, 20:43

I added these, and now oddly copying and pasting files/images no longer works:

Code: Select all

CleanGoogleLink() {
	OnClipboardChange("CleanGoogleLink", 0)
	Clipboard := RegExReplace(Clipboard, "google.com.*\K&oq=.*")
	Clipboard := RegExReplace(Clipboard, "fbcdn.net.*\K&bytestart=.*")
	Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
	Clipboard := StrReplace(Clipboard, "&source=lnt")

/*	if RegExMatch(Clipboard, "https://youtu.*?\K\d+$", Seconds) {
		Time = 20210101000000
		Time += Seconds, Seconds
		FormatTime, Time, %Time%, H:mm:ss
	Clipboard := Time
	}
	*/
		OnClipboardChange("CleanGoogleLink", 1)
}
User avatar
boiler
Posts: 16705
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?

Post by boiler » 26 Jul 2021, 20:48

Try this:

Code: Select all

CleanGoogleLink() {
	OnClipboardChange("CleanGoogleLink", 0)
	clipsave := ClipboardAll
	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=.*")
	if !Clipboard
		Clipboard := clipsave
	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?

Post by milkygirl90 » 27 Jul 2021, 02:40

Apologies.. just noticed there's another issue. When trying to cut or copy/paste files in Windows explorer, this script also prevents copy/paste.

In addition, it also seems to interfere with pasting formatted text in Evernote:

Image

Only unformatted text is pasted..
Post Reply

Return to “Ask for Help (v1)”