Page 1 of 1

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

Posted: 12 Jun 2017, 12:56
by SyntaxTerror
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.

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

Posted: 12 Jun 2017, 23:44
by boiler
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".

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

Posted: 13 Jun 2017, 01:24
by SyntaxTerror
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.

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

Posted: 13 Jun 2017, 06:11
by A_AhkUser
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).

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

Posted: 13 Jun 2017, 06:21
by boiler
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.

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

Posted: 13 Jun 2017, 06:31
by boiler
Try this:

Code: Select all

#Persistent
OnClipboardChange("CheckClip")
return

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

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

Posted: 13 Jun 2017, 07:16
by SyntaxTerror
Thanf you boiler & A_AhkUser. :)