AutoHotkey Community

It is currently May 27th, 2012, 1:54 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: January 24th, 2010, 2:37 pm 
Offline

Joined: February 7th, 2008, 9:48 pm
Posts: 509
Code:
F1::
Send,^x
ClipWait,1
data=%clipboard%
data:=regexreplace(data,"(- )(\w)","$1$T2")
pos:=regexmatch(data,"(\d[\d space]+\d)",subdata)
stringreplace,newsubdata,subdata1,%A_SPACE%,.,All
StringReplace, result,data,%subdata1%,%newsubdata%
clipboard=%result%
Send,^v
Return


There can be only one block of numbers in the selection!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2010, 2:51 pm 
This is very close, can't you make it work with mutli blocks of numbers?
Also I noticed that it will capitalize the words that come after - only if there is space in between

It will work on this:

- test = - Test


But not on this:

-test = -test


Can you solve that?

Please :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2010, 3:22 pm 
Offline

Joined: February 7th, 2008, 9:48 pm
Posts: 509
maybe this for the -test - test ?

Code:
F1::
Send,^x
ClipWait,1
data=%clipboard%
data:=regexreplace(data,"(-|- )(\w)","$1$T2")
pos:=regexmatch(data,"(\d[\d space]+\d)",subdata)
stringreplace,newsubdata,subdata1,%A_SPACE%,.,All
StringReplace, result,data,%subdata1%,%newsubdata%
clipboard=%result%
Send,^v
Return


You will have to wait for a more experienced forum member to solve the more than one block of numbers :)


Last edited by closed on January 24th, 2010, 4:19 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2010, 3:27 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Another regex:

Code:
oldstr = Nathaniel Branden 09 04 1930 - the first step toward change is awareness. The second step is acceptance.
newstr := RegExReplace(RegExReplace(oldstr, "((\d+)\s+(?=\d))", "$2."), "(\d.*?)([a-z])", "$1$u2")
MsgBox, %newstr%

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2010, 4:01 pm 
Offline

Joined: February 7th, 2008, 9:48 pm
Posts: 509
Hi Titan very nice but not there yet!

Code:
oldstr = Nathaniel Branden 09 04 4 5 1930 - the first step 04 4 5 toward change is awareness. The second step is acceptance.
newstr := RegExReplace(RegExReplace(oldstr, "((\d+)\s+(?=\d))", "$2."), "(\d.*?)([a-z])", "$1$u2")
MsgBox, %newstr%



After the second block of numbers : toward is changed in Toward

Quote:
---------------------------
A3656197.ahk
---------------------------
Nathaniel Branden 09.04.4.5.1930 - The first step 04.4.5 Toward change is awareness. The second step is acceptance.
---------------------------
OK
---------------------------


It is too complicated for me to see the solution!


update : maybe in two separate lines.

Code:
F1::
Send,^x
ClipWait,1
data=%clipboard%
data:=regexreplace(data,"(-|- )(\w)","$1$T2")
result:=RegExReplace(data, "((\d+)\s+(?=\d))", "$2.")
clipboard=%result%
Send,^v
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2010, 5:55 pm 
I love you yume, perfect :D
Thanks a million

And thanks to everyone who tried to help, wonderful people you are :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2010, 4:19 am 
Farmer wrote:
Well, I want it to work on whatever I have on my clipboard...

...you never said clipboard, you said "lot of documents"...so, at least, I assumed, if it's a text file, you would use AutoHotkey's Loop, Read to make the changes...if it's an Office or other non-text file, then of course you'd use the clipboard...

Farmer wrote:
...not to whats between...

...we know you don't wanna put the text to replace between ( )...but it's the best way to create a standalone example...in mine, at least, I called the variable "input"...so you should delete that section, then put whatever you want in the input var...

Farmer wrote:
...I made a mistake by giving that example in the first place.

...no, giving examples is fine/great, but be specific about the behavior you want & give multiple different examples...my RegEx will work on any string like...

Code:
  RegEx:             (\d{2})     \s     (\d{2})     \s     (\d{4})    (\s      -      \s)     (.)
Matches: <anything> <2-digits> <space> <2-digits> <space> <4-digits> <space> <dash> <space> <1-char> <anything>

...so if the rest of your lines don't follow that, then it won't work...you have 1 example, we assumed that the "Nathaniel Branden" would change, that the dates "09 04 1930" would change (but always be <2-digits> <space> <2-digits> <space> <4-digits>) & everything after " - " would change (note that's <space>
<dash><space> per your example)...

I'm kinda shocked you seem to have tested/used everyone's example but mine, I think mine is the clearest/easiest to edit...

Code:
output:=RegExReplace(input, regex, replace)

...that clearly shows what's happening on that line (take input var, filter thru regex, replacing as needed with contents of replace var, send result to output), instead of...

Code:
MsgBox,% RegExReplace(var,"(\d) (\d\d) (\d{4} - )([a-z]?)","$1.$2.$3$U4")

...which smashes everything on one line, making it a nightmare to read/learn/edit...

Anyway here's my new code, with the new points in mind...

Code:
#SingleInstance force
;// *** NOTE *** delete these lines...
;//input=
;//(LTrim
;//   Nathaniel Branden 09 04 1930 - the first step toward change is awareness. The second step is acceptance.
;//   Nathaniel Branden
;//)
;// *** NOTE *** replace with Hotkey definition...
F9::
;// *** NOTE *** ...& clipboard-getting code...
;// *** NOTE *** Backup Clipboard before messing with it...
cba:=ClipboardAll
;// *** NOTE *** Clear Clipboard so ClipWait works...
clipboard=
Send, ^c
ClipWait, 1
;// *** NOTE *** Error checking is good...
if (errorlevel) {
   ;// *** NOTE *** Restore Clipboard on error too...
   clipboard:=cba
   cba:=
   msgbox, 16, , Error getting contents of clipboard...did you select some text?
   return
}
;// *** NOTE *** put clipboard in my input var...
input:=clipboard

;///regex_digits=(?=\d)\s(?=\d)
;///regex_digits_replace=.
;///regex_digits=(\d+)\s(\d+)
;///regex_digits_replace=$1.$2
regex_digits=(\d)\s(?=\d)
regex_digits_replace=$1.
regex_dash_uppercase=(\s+-\s*)(.)
regex_dash_uppercase_replace=$1$u2
;// *** NOTE *** Copy input to output cuz I need to change it multiple times...
output:=input
;// *** NOTE *** I really don't know why it don't get em all in one pass, but whatever, 2 passes work...
Loop {
   output:=RegExReplace(output, regex_digits, regex_digits_replace, c)
   ;///msgbox, el(%errorlevel%) le(%A_LastError%) c(%c%)
   if (!c) {
      break
   }
}
output:=RegExReplace(output, regex_dash_uppercase, regex_dash_uppercase_replace, c)
;///msgbox, el(%errorlevel%) le(%A_LastError%) c(%c%)
;// *** NOTE *** no, I don't imagine you want a msgbox, but the output is in the output var, so change msgbox...
;//msgbox, 64, , ---input---`n%input%`n`n---output---`n%output%
;// *** NOTE *** ...to Send, {Raw} (raw because you don't want side effects like random key pressing)...
;//SendInput, {Raw}%output%
;// *** NOTE *** ...or put output in clipboard & Send, ^v...
clipboard:=output
Send, ^v
;// *** NOTE *** Restore Clipboard after messing with it...
clipboard:=cba
;// *** NOTE *** Clear backup var...
cba:=
return

/* Text for copy-paste testing...
**
** Nathaniel Branden 09 04 1930 - the first step toward change is awareness.
** Joe Schmoe 1 19 10 - really different example
** this is a test - file 3 3 3 77 6 54 4 5 6 7, please - help me
** what about this??? 1 2 4 5 7 testing testing testing - wow - wee - uppercase all of these?
** what about this??? 80 78 45 69 testing testing testing -wow -wee -uppercase all of these? (no spaces)
** test 1 2 3 4 test 5 7 8 9 test - testing
** test 10 20 30 40 test 50 70 80 90 test -testing
** upper-case me? upper -case me? upper - case me?
*/

...Titan/yume, I tried to do the (?=\d) in my tests just now (before noticing it in your code) & it didn't work, I'm posting this version & I'll try to figure out why my tests with it failed...I just got it before posting..."(?=\d)\s(?=\d)" don't work, but "(\d)\s(?=\d)" does? can't have 2 0-length assertions?...was trying to match & replace the space & not the digits...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2010, 4:38 am 
Farmer you can replace the loop...

Code:
;// *** NOTE *** I really don't know why it don't get em all in one pass, but whatever, 2 passes work...
Loop {
   output:=RegExReplace(output, regex_digits, regex_digits_replace, c)
   ;///msgbox, el(%errorlevel%) le(%A_LastError%) c(%c%)
   if (!c) {
      break
   }
}

...with just...

Code:
output:=RegExReplace(output, regex_digits, regex_digits_replace)

...that late regex change made the loop unnecessary...but I caught it after posting...(also it works fine with the loop, it's just unnecessary now)...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2010, 4:08 pm 
Quote:
I'm kinda shocked you seem to have tested/used everyone's example but mine, I think mine is the clearest/easiest to edit...


Sorry Guest, I was trying them all but got confused, but it all works great now, thanks a million my friend :)

I'm sorry but I have 2 more requests if someone wants to help...
Sometimes I have numbers next to words and no space in between and I need to make space. Example

test line2.55.25 test = test line 2.55.25 test
test line2.55.25test = test line 2.55.25 test

And I need to make space on some of my text, so I need to have a RegEx line that will space it and where I can comment it when not needed.

My second request is that I sometimes I need to put blocks of digits between (), example

test line2.55.25 test = test line (2.55.25) test test line (2.55.25) test

But I need also in a separate RegEx line because I don't want it to apply all the time.


Help would be great
I'm sorry for asking much, but RegEx is just too complicated for me


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2010, 4:16 pm 
Offline

Joined: June 6th, 2006, 3:19 pm
Posts: 1654
Location: Denmark
I use AHK RegEx SandBox to make my regex'ps

_________________
RegEx Powered Dynamic Hotstrings
COM
AutoHotkey 2


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 25th, 2010, 7:01 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
This works based on your one example, without and with parenthesis:

Code:
var=
(
test line2.55.25 test
test line 3.66.36test
)
MsgBox % RegExReplace(var,"([[:alpha:]]+(?=\d+)|\d+(?=[[:alpha:]]+))","$1 ")
MsgBox % RegExReplace(RegExReplace(var,"([[:alpha:]]+(?=\d+)|\d+(?=[[:alpha:]]+))","$1 "),"[\d\.]+","($0)")
return

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Bing [Bot], engunneer, sjc1000 and 20 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group