How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

19 Jan 2019, 21:19

Hi,

I'd appreciate your help.

How to manipulate clipboard to remove text after the word "References", keeping the remaining content for pasting?
The clipboard text has the word "References" always starting after a line break (first word of the line).

Example:
Text for test purposes. Text for test purposes.
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.

Desired output:
Text for test purposes. Text for test purposes.
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.

Regarding the script structure, I consider a shortcut to trigger the command, example:
^#c::
Send, ^c
COMMAND I NEED HELP FOR
Return
Thank you very much!
Last edited by cadudesun on 20 Jan 2019, 12:49, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

19 Jan 2019, 21:54

both of these examples are the exact same thing. what do u need done again?
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

19 Jan 2019, 22:03

Sorry. My mistake. I updated the initial thread.
Last edited by cadudesun on 20 Jan 2019, 12:49, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

19 Jan 2019, 23:08

Code: Select all

str = 
(
Text for test purposes. Text for test purposes. 
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.
)

MsgBox % SubStr(str, 1, RegExMatch(str, "`am)^References$") - 2)
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

20 Jan 2019, 12:42

Thanks a lot for the code @swagfag

I've adapted it to work with any text, and triggering the shortcut will insert the modified text (text removed from the word "References" ahead).

Code: Select all

#^v::
Clipboard= % SubStr(Clipboard, 1, RegExMatch(Clipboard, "`am)^References$") - 2)
Send, ^v
Return
I would appreciate further help to know if it is possible to insert "quotation marks" in the output text.

Example of the initial text:
Text for test purposes. Text for test purposes.
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.


Example of the text after using the command above:
Text for test purposes. Text for test purposes.
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.


Desired new output with "quotation marks":
Text for test purposes. Text for test purposes.
"Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes."


The quotation marks should be inserted only in the second text line.

By the way, I recorded my screen showing the use of the command in context: http://bit.ly/2FE0J7w

Thank you very much!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

20 Jan 2019, 21:00

i dont see the where the newlines are from the video, but if its only ever gonna be one line >> another line >> references >> another line, u can process it line by line:

Code: Select all

#^v::
	; delete this
	Clipboard = 
	(LTrim
		First line.
		Second line. Second line.
		References
		Third line. Third line. Third line. 
	)

	for each, line in StrSplit(Clipboard, "`n", "`r")
	{
		if (A_Index = 1)
			result .= line "`n"
		else if (A_Index = 2)
			result .= """" line """"
		else
			break
	}

	Clipboard := result
	Send, ^v
Return
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

21 Jan 2019, 11:45

Or like this:

Code: Select all

Clipboard = 
(
Text for test purposes. Text for test purposes. 
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.
)

MsgBox % Clipboard := RegExReplace(Clipboard, "s)([^`n`r]+)\RReferences.*", """$1""")
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

22 Jan 2019, 13:53

I like how different solutions with AutoHotkey are possible.

Example 1

Code: Select all

str = 
(
Text for test purposes. Text for test purposes. 
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.
)

Loop, parse, str, `n, `r
{
	If InStr(A_LoopField, "Reference")
	{
	break
	}
	else
	{
	Output1 .= A_LoopField "`n"
	}
}

msgbox % Output1
Example 2

Code: Select all

str = 
(
Text for test purposes. Text for test purposes. 
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.
)

Loop, parse, str, `n, `r
	{
		if (A_Index = 1)
			Output2 .= A_LoopField "`n"
		else if (A_Index = 2)
			Output2 .= """" A_LoopField """"
		 else if (A_Index > 2)
			break
	}
msgbox % Output2
Note- instead of str and Output, you could replace that with clipboard, and then press ^v or use Send, ^v to paste it
Newbie101
Posts: 5
Joined: 02 Jan 2022, 03:31

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

02 Jan 2022, 03:38

swagfag wrote:
19 Jan 2019, 23:08

Code: Select all

str = 
(
Text for test purposes. Text for test purposes. 
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.
)

MsgBox % SubStr(str, 1, RegExMatch(str, "`am)^References$") - 2)
Hi,
I have a similar request but I'm unable to figure it out.
I want to remove the text before a certain word from my clipboard.
In my case the I want to remove all text before the word onenote for this reason.
Thanks
User avatar
rommmcek
Posts: 1475
Joined: 15 Aug 2014, 15:18

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

02 Jan 2022, 04:45

Try:

Code: Select all

str = 
(
Text for test purposes. Text for test purposes. 
Text for test purposes. Text for test purposes. Text for test purposes. Text for test purposes.
References
Text for test purposes. Text for test purposes. Text for test purposes.
)

MsgBox % SubStr(str, RegExMatch(str, "`am)^References$"), StrLen(str))
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?  Topic is solved

02 Jan 2022, 06:33

Code: Select all

Clipboard = 
(
this that
the other onenote is
not this onenote
or that one
)
Sleep, 100
; -----------------------------------------
Clipboard := removeBeforeWord(Clipboard, "onenote")
Sleep, 100
MsgBox, 64, Result, %Clipboard%

removeBeforeWord(str, word) {
 RegExMatch(str, "\b\Q" word "\E\b.*", m)
 Return m
}
Newbie101
Posts: 5
Joined: 02 Jan 2022, 03:31

Re: How to manipulate clipboard to remove text after a certain "word", keeping the remaining for pasting?

02 Jan 2022, 07:18

mikeyww wrote:
02 Jan 2022, 06:33

Code: Select all

Clipboard = 
(
this that
the other onenote is
not this onenote
or that one
)
Sleep, 100
; -----------------------------------------
Clipboard := removeBeforeWord(Clipboard, "onenote")
Sleep, 100
MsgBox, 64, Result, %Clipboard%

removeBeforeWord(str, word) {
 RegExMatch(str, "\b\Q" word "\E\b.*", m)
 Return m
}
Awesome, works like a charm.
Thank you so much !! :superhappy: :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 212 guests