Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Trim leading and trailing white space from the clipboard


  • Please log in to reply
8 replies to this topic
alancantor
  • Members
  • 31 posts
  • Last active: Dec 23 2011 01:49 AM
  • Joined: 29 Mar 2010
I am looking to strip leading and training spaces, tabs, and maybe even CRs from the text in the clipboard. In VBA I accomplish this before copying to the clipboard:

Trim(Selection)

In AutoHotkey, I don't know how to act on a selection without copying it to the clipboard first. So, I begin by copying the text into the clipboard.

But how do I clean up something like this...

<space><enter><tab>Hello World!<space><enter><space><tab>

...so that the clipboard (or a variable, I suppose) contains just this:

Hello World!

rtcvb32
  • Members
  • 542 posts
  • Last active: Apr 04 2012 03:59 AM
  • Joined: 17 Feb 2008
Trimming is automatic (spaces and tabs) when assigning a variable with only = So...

;trim 'text'
text = %text%

However with newlines, it becomes a little trickier. Use regex.

;trim 'text'
text := regexreplace(text, "^\s+") ;trim beginning whitespace
text := regexreplace(text, "\s+$") ;trim ending whitespace



Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Edit: With more testing, it accualy removes the spaces in the middle of the text. Im no good at regex but I thought Id give it a try anyway.

This will remove all spaces around the text.
var := A_Space "`n" A_Tab "Hello World!" A_Space "`n" A_Space A_Tab
Msgbox "%var%"
var := RegExReplace(var, "[color=blue]([\s]*)([^\s]*)([\s]*)[/color]", "$2")
Msgbox, "%var%"
return

aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
I fixed it (Hopefuly). It now uses RegExMatch.
var := A_Space "`n" A_Tab "Hello World.  This is some text.  It should be matched."  A_Space "`n" A_Space A_Tab
Msgbox, 0, Before, "%var%"
N := "([^\s]+\s+[^\s])+"
RegExMatch(var, N, Match)
Msgbox, 0, After, "%Match%"

Edit: Made it match more than two spaces in the text.
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

rtcvb32
  • Members
  • 542 posts
  • Last active: Apr 04 2012 03:59 AM
  • Joined: 17 Feb 2008
Then that's odd. Because the match expression is very specifically spaces at the beginning and end of string. It's not multi-line so the anchors are not used in the middle... Interesting.

EDIT: Here's my test, i don't see a problem with my original regex.

;contains spaces and tabs, copy and paste should keep them
text =
(
    
				
Hello   World!   		
		   
)

;trim 'text'
msgbox '%text%'
text := regexreplace(text, "^\s+") ;trim beginning whitespace
msgbox '%text%'
text := regexreplace(text, "\s+$") ;trim ending whitespace
msgbox '%text%'


VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
As usual with regex, the two can be shortened into one:
Clipboard := "`n`n`r`t`t`n   `n  `tHello World`t`n`n`n`t`r`r`t   `t  "

msgbox % "-" clipboard "-"

Clipboard := RegexReplace( Clipboard, "^\s+|\s+$" )

msgbox % "-" clipboard "-"


rtcvb32
  • Members
  • 542 posts
  • Last active: Apr 04 2012 03:59 AM
  • Joined: 17 Feb 2008

As usual with regex, the two can be shortened into one:


True; I intentionally did that. In 'Mastering Regular Expressions', doing both in the same regex is slower, somewhere like 4 to 150 times slower than doing it as two steps. (off hand i can't remember the exact number)

EDIT: Hmmm did a few tests between the two regexes. They both perform the same, and under certain circumstances they both can outperform the other. I guess it's just preference at that point.

alancantor
  • Members
  • 31 posts
  • Last active: Dec 23 2011 01:49 AM
  • Joined: 29 Mar 2010
Thank you for your responses. I opted for the simplest solution: assign the clipboard to a variable, which strips the leading and trailing white spaces.

SendInput ^c                    ; Copy to clipboard
ClipWait 0.5                      ; Wait up to .5 seconds to populate clipboard
Clipboard = %Clipboard%  ; Trim left & right whitespaces

For now, the ability to strip out CRs is not so important.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
<!-- m -->https://ahknet.autoh... ... WhiteSpace<!-- m --> :wink: