| View previous topic :: View next topic |
| Author |
Message |
stuboo
Joined: 17 Apr 2007 Posts: 22
|
Posted: Fri Sep 19, 2008 11:41 am Post subject: Regex Replace n words with n ________ |
|
|
I have a small AHK script now that removes selected text from one input field, replaces it with ___________ and pastes it in a second input field. Here it is...
| Code: | F1:: ; Trigger button.
Send ^x
Send ______
Send {Tab}
Send ^v
Send {Enter}
return ; Exits trigger condition actions |
As it's written, all text that is cut gets replaced with a single _________ no matter how many words are cut. I'd like to determine how many words are in the text that is cut (by counting spaces with regex I suppose) and insert the same number of blanks.
In other words...
| Quote: | | The quick brown fox jumped over the lazy dog. |
would be...
| Quote: | | The ________ ________ _________ jumped over the lazy dog. |
instead of...
| Quote: | | The ________ jumped over the lazy dog. |
when I cut out...
I don't know how to write regular expressions and don't have time to learn (I'm a medical student who is using AHK along with Anki to make flashcards for studying during lecture. Adding this feature to my existing script would make things even more efficient.
Thanks in advance,
Ryan |
|
| Back to top |
|
 |
Serenity
Joined: 07 Nov 2004 Posts: 1271
|
Posted: Fri Sep 19, 2008 12:16 pm Post subject: |
|
|
You could do it with StringReplace. ErrorLevel stores the number of replaced items:
| Code: | F1:: ; Trigger button.
Send ^x
ClipWait
StringReplace, clipboard, clipboard, %A_Space%,, All
words := Errorlevel, str := "______ "
Loop % words
Send % str
Send {Tab}
Send ^v
Send {Enter}
return ; Exits trigger condition actions |
_________________ "Anything worth doing is worth doing slowly." - Mae West
 |
|
| Back to top |
|
 |
stuboo
Joined: 17 Apr 2007 Posts: 22
|
Posted: Fri Sep 19, 2008 1:05 pm Post subject: |
|
|
Serenity,
Thanks for the help, but it doesn't quite work.
It produces the following in inputOne
| Quote: | | The jumped over the lazy dog. |
and this in inputTwo
| Quote: | | Thequickbrownfoxjumpedoverthelazydog. |
It *should* produce this in inputOne
| Quote: | | The _______ ______ ______ jumped over the lazy dog. |
and this in inputTwo
_________________ ---
I used AutoHotKey to write this signature crazy fast. |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Fri Sep 19, 2008 5:58 pm Post subject: |
|
|
Tested and it works
| Code: | F1:: ; Trigger button.
Clipboard=
Send ^x
ClipWait, 1
StringReplace, clipboard, clipboard, %A_Space%,%A_Space%, UseErrorLevel ; don't remove the space put it back
Words:=ErrorLevel + 1 ; make sure to add one. Two words selected -> one space, e.g. loops needs to be done twice
Loop %Words%
Send ______{space}
Send {tab}
Send ^v
return ; Exits trigger condition actions |
_________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
stuboo
Joined: 17 Apr 2007 Posts: 22
|
Posted: Fri Sep 19, 2008 6:13 pm Post subject: Thanks! |
|
|
Awesome HugoV. It works like a charm. Thanks so much. _________________ ---
I used AutoHotKey to write this signature crazy fast. |
|
| Back to top |
|
 |
|