regex replace for a set of numbers in url

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

regex replace for a set of numbers in url

Post by newcod3r » 24 Oct 2021, 23:08

sample in and output
in: https://twitter.com/maskutsu/status/1452309437188870151
output: https://threadreaderapp.com/thread/1452309437188870151.html

Stuck here..

Code: Select all

Clipboard := RegExReplace(Clipboard, "`aim)twitter\.com\/[^\/]+\/status\/[0-9*]", "https://threadreaderapp.com/thread/")

Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: regex replace for a set of numbers in url

Post by Rohwedder » 25 Oct 2021, 02:39

Hallo,
RegExReplace only if StrReplace cannot. Do not use the slow clipboard as a working variable.
Clipboard := RegExReplace(Clipboard,
Try:

Code: Select all

in = https://twitter.com/maskutsu/status/1452309437188870151
should = https://threadreaderapp.com/thread/1452309437188870151.html
output := StrReplace(in, "twitter.com/maskutsu/status"
, "threadreaderapp.com/thread") ".html"
MsgBox,% should "`n" output

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: regex replace for a set of numbers in url

Post by newcod3r » 25 Oct 2021, 05:54

Hi there, what does "slow clipboard" mean?

I can't just use StrReplace because the username is variable, as such:

in = https://twitter.com/𝗧𝗵𝗶𝘀𝗜𝘀𝗔𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲/status/1452309437188870151

Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: regex replace for a set of numbers in url

Post by Rohwedder » 25 Oct 2021, 09:43

Not only Autohotkey has access to ClipBoard but also all other Windows processes.
If you share a paper notebook with 10 neighbors, you can't read and write in it at any time.
Instead of:

Code: Select all

ClipBoard := RegExReplace(ClipBoard, ...) ;read and write in ClipBoard
Send, ^v ;paste
better:

Code: Select all

Clip := ClipBoard
Sleep, 100
Clip := RegExReplace(Clip, ...) ;read and write in Clip
ClipBoard := Clip
Sleep, 100
Send, ^v ;paste
Perhaps:

Code: Select all

in = https://twitter.com/𝗧𝗵𝗶𝘀𝗜𝘀𝗔𝗩𝗮𝗿𝗶𝗮𝗯𝗹𝗲/status/1452309437188870151
should = https://threadreaderapp.com/thread/1452309437188870151.html
RegExMatch(in, "https://twitter.com/[^/]+/status/\K\d+", output)
output := "https://threadreaderapp.com/thread/" output ".html"
MsgBox,% should "`n" output

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: regex replace for a set of numbers in url

Post by teadrinker » 25 Oct 2021, 11:04

Rohwedder wrote: Instead of:
...
better:
I don't think the second code is really better. At least, in the docs there is an example:

Code: Select all

StringReplace, clipboard, clipboard, ABC, DEF, All   ; Replace all occurrences of ABC with DEF (also converts the clipboard to plain text).
I'd use in this case

Code: Select all

; Clipboard := "https://twitter.com/maskutsu/status/1452309437188870151"
Clipboard := RegExReplace(Clipboard, ".+?(\d+)$", "https://threadreaderapp.com/thread/$1.html")
Send ^v

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: regex replace for a set of numbers in url

Post by newcod3r » 25 Oct 2021, 18:57

Hi both,

Thank you for your help.

Would like to understand why there's a need to have .+? in front when \d+ already matches all the digits in regexreplace?
image.png
image.png (259.46 KiB) Viewed 854 times

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: regex replace for a set of numbers in url

Post by teadrinker » 25 Oct 2021, 20:49

This pattern .+?(\d+)$ specifies what the part of the haystack must be removed and replaced. If you were only using \d+ then just digitals would be replaced:

Code: Select all

url := "https://twitter.com/maskutsu/status/1452309437188870151"
MsgBox, % RegExReplace(url, "(\d+)$", "https://threadreaderapp.com/thread/$1.html")

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: regex replace for a set of numbers in url

Post by amateur+ » 25 Oct 2021, 22:55

Rohwedder wrote:

Code: Select all

RegExMatch(in, "https://twitter.com/[^/]+/status/\K\d+", output)
Hi! I think, RegExMatch(in, "\d+$", output) would be a little bit shorter.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: regex replace for a set of numbers in url

Post by newcod3r » 26 Oct 2021, 19:22

Thanks all!

Post Reply

Return to “Ask for Help (v1)”