Page 1 of 1

Fastest way to replace text?

Posted: 15 May 2016, 21:56
by Dorian2
Hi!
I just started reading about RegEx. I would like to know if it is the fastest way to replace about 500 strings in whole txt files of 0.5GB. Something like:

1) aabbccddee = aa_e
2) bbccddeeff = bb_f
3) wwxxyyzz = w!
4) EwExEyEz = Ew!
5) EwExEy = Ew_y
6) DaDfDhDz = D(afhz)
...
500) abbccddeeffgh = ab_gh

Currently i am using StringReplace, but it takes several minutes :/
Thank you so much for your help!

Re: Fastest way to replace text?

Posted: 15 May 2016, 23:31
by boiler
StringReplace is faster than RegExReplace.

Re: Fastest way to replace text?

Posted: 16 May 2016, 00:30
by gilliduck
The speed issue you're having may not be with the replacing, it may be with the writing. How are you doing it exactly? Reading the file into memory, replacing all the text, deleting the file, writing the file?

Re: Fastest way to replace text?

Posted: 16 May 2016, 08:28
by Dorian2
I am using A_TickCount for both processes.
Right now it takes 4min to read:
- FileRead
- Loop every row, replace some text and make lists of groups

2min:
- Do most of the text replace
- I used this method:

Code: Select all

ListLong = AABBCCDDEE, AABBCCDD, AABBCC, AABB, etc
Loop, Parse, ListLong, `,
   ListLong%A_Index% := A_LoopField

ListShort = A-E, A-D, A-C, A-B, etc
Loop, Parse, ListShort, `,
   ListShort%A_Index% := A_LoopField

Loop, parse, RowsOfText, `n {
  NewRow := A_LoopField
  StringReplace, NewRow , NewRow , ListLong%A_Index%,  ListShort%A_Index%, A
}
- FileAppend: I tried every X rows, and make a whole variable (MonsterVar = % MonsterVar NewText)... Save every X rows gives me a little better result.



Thanks a lot!!

Re: Fastest way to replace text?

Posted: 16 May 2016, 08:38
by rootmos
Hi, im not sure if it's faster or not, but im using this lib: https://github.com/hi5/TF

Re: Fastest way to replace text?

Posted: 16 May 2016, 08:49
by boiler
Instead of looping, can't you just read it all into one variable and do a StringReplace...All on the whole thing for each piece of text to be replaced? That would be very fast.

Re: Fastest way to replace text?

Posted: 16 May 2016, 09:00
by rootmos
boiler wrote:Instead of looping, can't you just read it all into one variable and do a StringReplace...All on the whole thing for each piece of text to be replaced? That would be very fast.
If the text contains more than 256 characters the variable will be to long.

Code: Select all

https://autohotkey.com/docs/Variables.htm
Variable names: Variable names are not case sensitive (for example, CurrentDate is the same as currentdate). Variable names may be up to 253 characters long and may consist of letters, numbers and the following punctuation: # _ @ $

Re: Fastest way to replace text?

Posted: 16 May 2016, 09:37
by Coco
rootmos wrote:If the text contains more than 256 characters the variable will be to long.

Code: Select all

https://autohotkey.com/docs/Variables.htm
Variable names: Variable names are not case sensitive (for example, CurrentDate is the same as currentdate). Variable names may be up to 253 characters long and may consist of letters, numbers and the following punctuation: # _ @ $
Variable names are limited to 253 chars but variable contents are not. variable_name := "variable content"

Re: Fastest way to replace text?

Posted: 16 May 2016, 09:41
by guest3456
you may want to post your full code and input file

Re: Fastest way to replace text?

Posted: 16 May 2016, 10:15
by boiler
I routinely use StringReplace with variables that contain thousands of characters.

Re: Fastest way to replace text?

Posted: 17 May 2016, 21:54
by Dorian2
Thank you guys. I will try what boiler said first. I can't post the code, it is way too long.