Editing the content of the clipboard with a regular expression (RegExReplace & Clipboard) Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
SyntaxTerror
Posts: 24
Joined: 23 May 2017, 12:55

Editing the content of the clipboard with a regular expression (RegExReplace & Clipboard)

12 Jun 2017, 12:56

Hello

I am trying to make a script to edit the content of the clipboard with a regular expression.

In short, I want to copy "a:1,2,3;b:4,5,6;c:7,8,9;d:1,2,3;e:4,5,6;f:7,8,9" and get "b:4,5,6;d:1,2,3;e:4,5,6" when I paste.

I tried using RegExReplace and the Clipboard variable, but I cannot get my script to work (I guess it is due to syntax problems, like the use of quotation marks, or maybe the incorrect use of variables).
I would like to make something like this:

Code: Select all

Clipboard :=  RegExReplace(Clipboard,"a.*(b.*);c.*(;d.*);f.*","$1$2")
return
(the problem does not seem to come from the regex i use, I tested it on https://regex101.com/)

Thank you for you help.
User avatar
boiler
Posts: 6124
Joined: 21 Dec 2014, 02:44

Re: Editing the content of the clipboard with a regular expression (RegExReplace & Clipboard)

12 Jun 2017, 23:44

It works for me. You already have "a:1,2,3;b:4,5,6;c:7,8,9;d:1,2,3;e:4,5,6;f:7,8,9" copied to the clipboard when you run the script, right? And when you paste after running the script, what does it show? For me, it pastes "b:4,5,6;d:1,2,3;e:4,5,6".
User avatar
SyntaxTerror
Posts: 24
Joined: 23 May 2017, 12:55

Re: Editing the content of the clipboard with a regular expression (RegExReplace & Clipboard)

13 Jun 2017, 01:24

Hello boiler and thank you for you quick answer.
boiler wrote:It works for me. You already have "a:1,2,3;b:4,5,6;c:7,8,9;d:1,2,3;e:4,5,6;f:7,8,9" copied to the clipboard when you run the script, right? And when you paste after running the script, what does it show? For me, it pastes "b:4,5,6;d:1,2,3;e:4,5,6".
Yes... In fact I was thinking that I had to lauch the script BEFORE copying, but it actually works if I launch it AFTER copying it!

Is there is a way to make this script work everytime I copy a string that matches the regex?
i.e. I want the script to be lauched when Windows starts and everytime I copy something looking like "a:1,2,3;b:4,5,6;c:7,8,9;d:1,2,3;e:4,5,6;f:7,8,9", it is changed to "b:4,5,6;d:1,2,3;e:4,5,6" when I paste it.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Editing the content of the clipboard with a regular expression (RegExReplace & Clipboard)

13 Jun 2017, 06:11

Hi SyntaxTerror,
SyntaxTerror wrote:Is there is a way to make this script work everytime I copy a string that matches the regex?
You can register a function to run whenever the clipboard's content changes ( :arrow: OnClipboardChange).
my scripts
User avatar
boiler
Posts: 6124
Joined: 21 Dec 2014, 02:44

Re: Editing the content of the clipboard with a regular expression (RegExReplace & Clipboard)

13 Jun 2017, 06:21

If you launch it before copying, it just acts on whatever is in the clipboard at that time and ends.

This would work if you wanted to launch it before copying because it clears the clipboard and waits for it to contain text:

Code: Select all

Clipboard := ""
ClipWait
Clipboard :=  RegExReplace(Clipboard,"a.*(b.*);c.*(;d.*);f.*","$1$2")
return
I'll write something that should act the way you want.
User avatar
boiler
Posts: 6124
Joined: 21 Dec 2014, 02:44

Re: Editing the content of the clipboard with a regular expression (RegExReplace & Clipboard)  Topic is solved

13 Jun 2017, 06:31

Try this:

Code: Select all

#Persistent
OnClipboardChange("CheckClip")
return

CheckClip()
{
	result :=  RegExReplace(Clipboard,"a.*(b.*);c.*(;d.*);f.*","$1$2")
	if result
		Clipboard := result
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, AlexL, BGM, evilmanimani, jarhead, keylo, mikeyww, smogmanus and 53 guests