regex help for replacement

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 help for replacement

Post by milkygirl90 » 21 Sep 2021, 19:52

Sample input

Code: Select all

https://www.facebook.com/groups/1016605764059/search?q=test&filters=eyJycF9jaHJvbm9fc29ydDowIjoie1wibmFtZVwiOlwiY2hyb25vc29ydFwiLFwiYXJnc1wiOlwiXCJ9In0%3D
output

Code: Select all

https://www.facebook.com/groups/1016605764059/search?q=%s&filters=eyJycF9jaHJvbm9fc29ydDowIjoie1wibmFtZVwiOlwiY2hyb25vc29ydFwiLFwiYXJnc1wiOlwiXCJ9In0%3D
my current code

Code: Select all

Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
this removes everything after %s, which I don't want it to happen.

1. How do I do this simple replacement?
2. In addition, how do I restrict the code below to work only if "search?q=" is not present? otherwise it will conflict with my code above.

Code: Select all

		Clipboard := RegExReplace(Clipboard, "`aim)facebook.com/groups/[^/]+/\K(.*)", "?sorting_setting=CHRONOLOGICAL")

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: regex help for replacement

Post by AHKStudent » 21 Sep 2021, 20:05

On your first question your regex seems to give me the output you want

[Codebox=autourl := "https://www.facebook.com/groups/1016605764059/search?q=test&filters=eyJycF9jaHJvbm9fc29ydDowIjoie1wibmFtZVwiOlwiY2hyb25vc29ydFwiLFwiYXJnc1wiOlwiXCJ9In0%3D"
MsgBox, % RegExReplace(url, "(?<=search\?q=)[a-z\+]+", "%s")
ExitApphotkey file=Untitled.ahk][/Codebox]

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: regex help for replacement

Post by Xtra » 21 Sep 2021, 20:30

Using your sample input and current code: https://regex101.com/r/oLCQ8s/1 You can see its working correctly.
Your 2nd question you could check if its InStr() or not and then make the appropriate action.

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: regex help for replacement

Post by milkygirl90 » 21 Sep 2021, 22:37

Xtra wrote:
21 Sep 2021, 20:30
Using your sample input and current code: https://regex101.com/r/oLCQ8s/1 You can see its working correctly.
Your 2nd question you could check if its InStr() or not and then make the appropriate action.
For Q2, is it possible to achieve this within the clipboard := regexreplace line? I already have a couple of !Instr and this will complicate the code even more..

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: regex help for replacement

Post by milkygirl90 » 21 Sep 2021, 22:49

AHKStudent wrote:
21 Sep 2021, 20:05
On your first question your regex seems to give me the output you want

[Codebox=autourl := "https://www.facebook.com/groups/1016605764059/search?q=test&filters=eyJycF9jaHJvbm9fc29ydDowIjoie1wibmFtZVwiOlwiY2hyb25vc29ydFwiLFwiYXJnc1wiOlwiXCJ9In0%3D"
MsgBox, % RegExReplace(url, "(?<=search\?q=)[a-z\+]+", "%s")
ExitApphotkey file=Untitled.ahk][/Codebox]
hmm there must be something wrong because my code only produces the correct output half the time.

Code: Select all

f10:: ;Add New Search Engine Shortcut in Google Chrome
OnClipboardChange("CleanLink", 0)
ClipboardOld := ClipboardAll
Clipboard := ""
SendInput !d
Sleep 80
SendInput ^c	
ClipWait, 3
Sleep 3700
Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
Sleep 1000				
msgbox, %clipboard%
input

Code: Select all

https://www.google.com/search?q=agawga
output not replaced.

Code: Select all

https://www.google.com/search?q=agawga

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: regex help for replacement

Post by Xtra » 21 Sep 2021, 23:36

Using more Instr() to get it to work as you intend is not an issue. Dont over think things trying to all in one regex or one liner any code. The larger the code gets the more you should be making it readable to be able to maintain and debug it. (imo)

Take a look again your last input posted works: https://regex101.com/r/RGW1a4/1

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: regex help for replacement

Post by milkygirl90 » 22 Sep 2021, 09:17

yeah the regex itself looks correct now, but I'm not sure why my code above doesn't work as intended..

Code: Select all

f10:: ;Add New Search Engine Shortcut in Google Chrome
OnClipboardChange("CleanLink", 0)
ClipboardOld := ClipboardAll
Clipboard := ""
SendInput !d
Sleep 80
SendInput ^c	
ClipWait, 3
Sleep 3700
Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
Sleep 1000				
msgbox, %clipboard%

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: regex help for replacement

Post by boiler » 22 Sep 2021, 10:10

Perhaps the issue is with sending the ^c not always working. Try with the more robust method below, along with the feedback letting you know if that's the case:

Code: Select all

f10:: ;Add New Search Engine Shortcut in Google Chrome
OnClipboardChange("CleanLink", 0)
ClipboardOld := ClipboardAll
Clipboard := ""
SendInput !d
Sleep 80
SendInput {Ctrl down}c{Ctrl up}
ClipWait, 3
if ErrorLevel {
	MsgBox, Nothing copied to clipboard
	return
}
Clipboard := RegExReplace(Clipboard, "(?<=search\?q=)[a-z\+]+", "%s")
Sleep 1000				
msgbox, %clipboard%
Clipboard := ClipboardOld ; since you created it, might as well restore it
OnClipboardChange("CleanLink", 1) ; assume you want this turned back on
return

Post Reply

Return to “Ask for Help (v1)”