AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Regex Replace n words with n ________

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
stuboo



Joined: 17 Apr 2007
Posts: 22

PostPosted: Fri Sep 19, 2008 11:41 am    Post subject: Regex Replace n words with n ________ Reply with quote

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...
Quote:
quick brown fox


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
View user's profile Send private message
Serenity



Joined: 07 Nov 2004
Posts: 1271

PostPosted: Fri Sep 19, 2008 12:16 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
stuboo



Joined: 17 Apr 2007
Posts: 22

PostPosted: Fri Sep 19, 2008 1:05 pm    Post subject: Reply with quote

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
Quote:
quick brown fox

_________________
---
I used AutoHotKey to write this signature crazy fast.
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Fri Sep 19, 2008 5:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
stuboo



Joined: 17 Apr 2007
Posts: 22

PostPosted: Fri Sep 19, 2008 6:13 pm    Post subject: Thanks! Reply with quote

Awesome HugoV. It works like a charm. Thanks so much.
_________________
---
I used AutoHotKey to write this signature crazy fast.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group