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...