poopup wrote:
i'm solved it by use this..
Code:
RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")
^^
That might be a good solution but if you have a large number of lines in the file, you will find that using ahk's string commands and functions
much faster.
By replacing the above regex
Code:
Trimmed1 := RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")
with
Code:
StringGetPos,lastNewLine, DataText,`n,r,1
Trimmed1 := SubStr(DataText,firstNewLine := Instr(DataText,"`n")+1,lastNewLine-firstNewLine)
you will find that for small files (10 lines) using regex is 2 times slower that string commands. For files containing more lines (or longer lines), regex is about 80 times slower than string commands. While the code below is not a thorough benchmark, run it several times to see the speed advantage of the solution using ahk's string commands.
Code:
#SingleInstance, force
SetBatchLines, -1
lines = 10
header = results for %lines% lines of data`n`n
loop, %lines% ; create test data set
DataText .= "line " A_Index "`r`n"
msgbox, Input test data`n`n>>%DataText%<<
gosub, compareTime
msgbox, First and last line trim %header%string result`n`n>>%Trimmed1%<<`n`n`n`nregex result`n`n>>%Trimmed2%<<
result = Time %header%%Time_strings% seconds using string functions`n%Time_regex% seconds using regex
result .= "`n`nRegex Time / String Time = " Time_regex / Time_strings
lines = 10000
msgbox, %result%`n`n`nPress OK to start %lines% line test
DataText =
header = Results for %lines% lines of data`n`n
loop, %lines% ; create test data set
DataText .= "line " A_Index "`r`n"
;msgbox, data set`n`n>>%data%<<
gosub, compareTime
result = Time %header%%Time_strings% seconds using string functions`n%Time_regex% seconds using regex
result .= "`n`nRegex Time / String Time = " Time_regex / Time_strings
msgbox, %result%
ExitApp
compareTime:
; get time for data trim using string instructions
T() ; start timer
StringGetPos,lastNewLine, DataText,`n,r,1
Trimmed1 := SubStr(DataText,firstNewLine := Instr(DataText,"`n")+1,lastNewLine-firstNewLine)
Time_strings := T() ; get elapsed time
; get time for data trim using regex instructions
T() ; start timer
Trimmed2 := RegExReplace(DataText,"(^.*\r?\R|\R[^\n\r\r]*$)")
Time_regex := T() ; get elapsed time
return
T() {
Static freq, last_count
if !freq
DllCall("QueryPerformanceFrequency","int64*",freq)
DllCall("QueryPerformanceCounter","int64*",count)
return (count-last_count)/freq, last_count:=count
}
You can see a more thorough benchmark comparing string commands and regex in this post
TIP - When to NOT use RegEx