Replacement with variables?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
4434523

Replacement with variables?

06 Dec 2017, 07:06

I search a solution for following replacements: (with RegExReplace?)

case 1: abc123/yz_
case 2: abc1234/yz_
case 3: abc12345/yz_

the number could have even more digits (case x)

until now I have:

Code: Select all

#Persistent
return
OnClipboardChange:
StringReplace, clipboard, clipboard, abc123/xy_, replacement, All
RETURN
which only works for case 1 and only with the same numbers

any ideas?
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Replacement with variables?

06 Dec 2017, 08:14

Code: Select all

ClipBoard := RegExReplace(ClipBoard, "(?<=case )(\d*:) abc\d+/yz_", "$1")
Please excuse my spelling I am dyslexic.
32234234

Re: Replacement with variables?

06 Dec 2017, 10:43

thanks for your answer,

if I copy some text (Ctrl+C) with this code enabled via ahk:

Code: Select all

#Persistent
return
OnClipboardChange:
ClipBoard := RegExReplace(ClipBoard, "(?<=case )(\d*:) abc\d+/yz_", "test")
RETURN
and then try to paste with Ctrl+V, nothing happens...?
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Replacement with variables?

06 Dec 2017, 11:02

It works for me with your provided sample text.
Please excuse my spelling I am dyslexic.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Replacement with variables?

06 Dec 2017, 11:19

Capn Odin, I suppose, you don't need to include "case 1: " part.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Replacement with variables?

06 Dec 2017, 12:03

I think that it will be easier if you explain exactly what you aim to replace in the string.
Please excuse my spelling I am dyslexic.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Replacement with variables?

06 Dec 2017, 12:26

Code: Select all

#Persistent
replacement := "test"
Return

OnClipboardChange:
   Clipboard := RegExReplace(Clipboard, "abc\d+/[a-z]{2}_", replacement)
   Return
474333

Re: Replacement with variables?

10 Dec 2017, 13:30

thanks teadrinker, this way it works..

I have an additional question: how it's possible that the script replaces the right command if I have more than one command? I tried following, but it doesn't work:

Code: Select all

#Persistent
replacement1 := "test1"
Return

OnClipboardChange:
   Clipboard := RegExReplace(Clipboard, "abc\d+/[a-z]{2}_", replacement1)
   Return
   
replacement2 := "test2"
Return

OnClipboardChange:
   Clipboard := RegExReplace(Clipboard, "abcdef\d+/[a-z]{2}_", replacement2)
   Return
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Replacement with variables?

10 Dec 2017, 15:46

I'm not sure, what result do you want to get? How the script should work? When it should change the replacement?
474333

Re: Replacement with variables?

10 Dec 2017, 17:10

in the above example there are 3 cases possible:
1: if (by regex-method) "abc\d+/[a-z]{2}_" is found in the clipboard, it should replace to "test1"
2: if (by regex-method) "abcdef\d+/[a-z]{2}_" is found in the clipboard, it should replace to "test2"
3: nothing was found, nothing has to be replaced

on principle it doesnt matter if it is regex it could be stringreplace-method too
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Replacement with variables?

11 Dec 2017, 01:29

Code: Select all

#Persistent
replacement1 := "test1"
replacement2 := "test2"
Return

OnClipboardChange:
   Clipboard := RegExReplace(Clipboard, "abc\d+/[a-z]{2}_", replacement1)
   Clipboard := RegExReplace(Clipboard, "abcdef\d+/[a-z]{2}_", replacement2)
   Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: bobstoner289, Chunjee, Google [Bot], macromint, peter_ahk and 328 guests