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!
Trim leading and trailing white space from the clipboard
Started by
alancantor
, Sep 08 2010 09:51 PM
8 replies to this topic
#1
-
Posted 08 September 2010 - 09:51 PM
Trimming is automatic (spaces and tabs) when assigning a variable with only = So...
However with newlines, it becomes a little trickier. Use regex.
;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
#2
-
Posted 08 September 2010 - 09:59 PM
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.
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
#3
-
Posted 08 September 2010 - 11:34 PM
aboutscript ⋰ apps ⋱ scripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
I fixed it (Hopefuly). It now uses RegExMatch.
Edit: Made it match more than two spaces in the text.
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.
#4
-
Posted 08 September 2010 - 11:54 PM
aboutscript ⋰ apps ⋱ scripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
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.
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%'
#5
-
Posted 09 September 2010 - 12:14 AM
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 "-"
#6
-
Posted 09 September 2010 - 01:29 AM
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.
#7
-
Posted 09 September 2010 - 02:37 AM
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.
For now, the ability to strip out CRs is not so important.
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.
#8
-
Posted 09 September 2010 - 02:52 AM
<!-- m -->https://ahknet.autoh... ... WhiteSpace<!-- m --> :wink:
#9
-
Posted 09 September 2010 - 07:03 AM