(SOLVED) Simple Encrypter _ StringReplace - Prevent replacing strings already replaced!
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
(SOLVED) Simple Encrypter _ StringReplace - Prevent replacing strings already replaced!
_______________________________________________________
Simple Encrypter Test!
(191 Characters in Total used in Encryption process)
https://autohotkey.com/boards/viewtopic ... 503#p86503
Simple Encrypter Test - 2!
(Only A to Z Uppercase Letters are used in the Encryption process! - 26 Characters in Total)
https://autohotkey.com/boards/viewtopic ... 872#p88872
Gui EOL (End Of Line) Tranlsation Issue (`r`n)!
Solution: https://autohotkey.com/boards/viewtopic ... 503#p86503
_______________________________________________________
(SOLUTION):
94_ https://autohotkey.com/boards/viewtopic ... 201#p86201
Use one Delimiter only "#" (Thanks to "AlphaBravo"!)
- "CaseSense" Parameter Added (Turn off Case-Sensitive)
- Bugs from solution 95 (Fixed - Thanks to "just me" advice!)
Limitation: same as solution 98
95_ https://autohotkey.com/boards/viewtopic ... 564#p85564
Use one Delimiter only "#" (Thanks to "AlphaBravo"!)
- Bug from solution 96 (Fixed - Thanks to "AlphaBravo"!)
- Bug Detected: 95_ link (some issues related with Case Sensitive/Insensitive that need to be fixed!)
Limitation: same as solution 98
96_ https://autohotkey.com/boards/viewtopic ... 117#p85117
Uses one Delimiter only "#" (Thanks to "Masonjar13" Help!)
(AlphaBravo code implemented in silvex3000 function)
Bug Detected: 96 link
Limitation: same as solution 98
97_ https://autohotkey.com/boards/viewtopic ... 721#p84721
(AlphaBravo code implemented in silvex3000 function)
Limitation: same as solution 98
98_ https://autohotkey.com/boards/viewtopic ... 446#p84446 (silvex3000)
Limitation: same link
99_ https://autohotkey.com/boards/viewtopic ... 689#p83689 (AlphaBravo)
Limitation: https://autohotkey.com/boards/viewtopic ... 724#p83724
For those who helped, Thanks!
(Others solutions are welcome!)
_______________________________________________________
Text = ABCD
(Red strings should not be replaced)
StringReplace, Text, Text, A, B, all
BBCD
StringReplace, Text, Text, B, A, all (The first "B" should not be replaced because its color is red)
BACD
StringReplace, Text, Text, C, D, all
BADD
StringReplace, Text, Text, D, C, all (The first "D" should not be replaced because its color is red)
BADC
StringReplace, Text, Text, BADC, ZXYW, all ("BADC" should not be replaced because its color is red)
BADC
is there any easy workaround for this?
Simple Encrypter Test!
(191 Characters in Total used in Encryption process)
https://autohotkey.com/boards/viewtopic ... 503#p86503
Simple Encrypter Test - 2!
(Only A to Z Uppercase Letters are used in the Encryption process! - 26 Characters in Total)
https://autohotkey.com/boards/viewtopic ... 872#p88872
Gui EOL (End Of Line) Tranlsation Issue (`r`n)!
Solution: https://autohotkey.com/boards/viewtopic ... 503#p86503
_______________________________________________________
(SOLUTION):
94_ https://autohotkey.com/boards/viewtopic ... 201#p86201
Use one Delimiter only "#" (Thanks to "AlphaBravo"!)
- "CaseSense" Parameter Added (Turn off Case-Sensitive)
- Bugs from solution 95 (Fixed - Thanks to "just me" advice!)
Limitation: same as solution 98
95_ https://autohotkey.com/boards/viewtopic ... 564#p85564
Use one Delimiter only "#" (Thanks to "AlphaBravo"!)
- Bug from solution 96 (Fixed - Thanks to "AlphaBravo"!)
- Bug Detected: 95_ link (some issues related with Case Sensitive/Insensitive that need to be fixed!)
Limitation: same as solution 98
96_ https://autohotkey.com/boards/viewtopic ... 117#p85117
Uses one Delimiter only "#" (Thanks to "Masonjar13" Help!)
(AlphaBravo code implemented in silvex3000 function)
Bug Detected: 96 link
Limitation: same as solution 98
97_ https://autohotkey.com/boards/viewtopic ... 721#p84721
(AlphaBravo code implemented in silvex3000 function)
Limitation: same as solution 98
98_ https://autohotkey.com/boards/viewtopic ... 446#p84446 (silvex3000)
Limitation: same link
99_ https://autohotkey.com/boards/viewtopic ... 689#p83689 (AlphaBravo)
Limitation: https://autohotkey.com/boards/viewtopic ... 724#p83724
For those who helped, Thanks!
(Others solutions are welcome!)
_______________________________________________________
Text = ABCD
(Red strings should not be replaced)
StringReplace, Text, Text, A, B, all
BBCD
StringReplace, Text, Text, B, A, all (The first "B" should not be replaced because its color is red)
BACD
StringReplace, Text, Text, C, D, all
BADD
StringReplace, Text, Text, D, C, all (The first "D" should not be replaced because its color is red)
BADC
StringReplace, Text, Text, BADC, ZXYW, all ("BADC" should not be replaced because its color is red)
BADC
is there any easy workaround for this?
Last edited by silvex3000 on 25 May 2016, 19:04, edited 22 times in total.
Re: StringReplace - Don't replace strings already replaced?
Assign the first text to something not in your script (a place holder). Replace the rest and then come back through and replace the first with the proper text.
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: StringReplace - Don't replace strings already replaced?
An practical example with codes would be welcome!punchin wrote:Assign the first text to something not in your script (a place holder). Replace the rest and then come back through and replace the first with the proper text.
Re: StringReplace - Don't replace strings already replaced?
Or use numbers instead of letters and then as final step convert the numbers back to letters.
Note that loops can be used instead if you want to avoid redundant code
A = 1
B = 2
C = 3
D = 4
...
Code: Select all
Text = ABCD
StringReplace, Text, Text, A, 2, all
2BCD
StringReplace, Text, Text, B, 1, all
21CD
StringReplace, Text, Text, C, 4, all
21C4
StringReplace, Text, Text, D, 3, all
2134
StringReplace, Text, Text, BADC, ZXYW, all
2134
Code: Select all
StringReplace, Text, Text, 1, A, All
StringReplace, Text, Text, 2, B, All
StringReplace, Text, Text, 3, C, All
StringReplace, Text, Text, 4, D, All
Re: StringReplace - Don't replace strings already replaced?
Code: Select all
pos := 1
Text = ABCD
if (InStr(Text, "A", 1, pos, 1))
Text := RegExReplace(Text, "A", "B", 0, 1, pos++) ; BBCD
if (InStr(Text, "B", 1, pos, 1))
Text := RegExReplace(Text, "B", "A", 0, 1, pos++) ; BACD
if (InStr(Text, "C", 1, pos, 1))
Text := RegExReplace(Text, "C", "D", 0, 1, pos++) ; BADD
if (InStr(Text, "D", 1, pos, 1))
Text := RegExReplace(Text, "D", "C", 0, 1, pos++) ; BADC
if (InStr(Text, "BADC", 1, pos, 1))
Text := RegExReplace(Text, "BADC", "ZXYW", 0, 1, pos) ; BADC
MsgBox % Text
Code: Select all
Text = ABCD
Loop, Parse, Text
NewText .= A_LoopField = "A" ? "B" : A_LoopField = "B" ? "A" : A_LoopField = "C" ? "D" : A_LoopField = "D" ? "C" : A_LoopField
MsgBox % NewText
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: StringReplace - Don't replace strings already replaced?
Thanks for the replies, but this is what I really need:
Code: Select all
Text = ABA
(strings between " and ` should not be replaced!)
if stringexist, B
{
Find Left of B, " or `
if found = `
{
Replace B to "A`
return
}
if found = "
return ;B should not be replaced to "A`
Find Right of B, " or `
if found = "
{
Replace B to "A`
return
}
if found = `
return ;B should not be replaced to "A`
Replace B to "A` ;replace anyway if no " or ` is found from the Left or from the Right of B
return
}
Text = A"A`A ;after replacing B to "A`
if stringexist, A
{
Find Left of A, " or `
if found = `
{
Replace A to "B`
return
}
if found = "
return ;A should not be replaced to "B`
Find Right of A, " or `
if found = "
{
Replace A to "B`
return
}
if found = `
return ;A should not be replaced to "B`
Replace A to "B` ;replace anyway if no " or ` is found from the Left or from the Right of A
return
}
Text = "B`"A`"B` ;after replacing A to "B`
stringreplace " to blank
stringreplace ` to blank
Text = BAB ;after replacing " and ` to blank
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: StringReplace - Don't replace strings already replaced?
How to replace only the third occurrence?
Text = ABCZZZZZABCZZZZZZZABC
stringReplace, Text, Text, ABC, G, 3 ;3 for third occurrence but doesn't work
Text = ABCZZZZZABCZZZZZZZG
Text = ABCZZZZZABCZZZZZZZABC
stringReplace, Text, Text, ABC, G, 3 ;3 for third occurrence but doesn't work
Text = ABCZZZZZABCZZZZZZZG
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: StringReplace - Don't replace strings already replaced?
Came up with this (search to the "left" of a "string" occurrence):
works great, but uses "gosub" instead of "function"
this is what the code does:
A - 000 - B - 111 - A - 222 - B - 333 - A - 444 - B - 555 - A - 666 - B - 777 - A - 888 - B - 999 - A
String1 = A
String2 = "B``
gosub, ReplaceString
"B` - 000 - B - 111 - "B` - 222 - B - 333 - "B` - 444 - B - 555 - "B` - 666 - B - 777 - "B` - 888 - B - 999 - "B`
String1 = B
String2 = "A``
gosub, ReplaceString ;Bs between " and ` will not be replaced
"B` - 000 - "A`` - 111 - "B` - 222 - "A`` - 333 - "B` - 444 - "A`` - 555 - "B` - 666 - "A`` - 777 - "B` - 888 - "A`` - 999 - "B`
stringreplace, text, text, ", , all
stringreplace, text, text, ``, , all
B - 000 - A - 111 - B - 222 - A - 333 - B - 444 - A - 555 - B - 666 - A - 777 - B - 888 - A - 999 - B ;final result
with "gosub" works great, but an easier workaround would be welcome!!!
_______________________________________________________________
I tried to use a function (but didn't work):
this is how the function should work:
A - 000 - B - 111 - A - 222 - B - 333 - A - 444 - B - 555 - A - 666 - B - 777 - A - 888 - B - 999 - A
ReplaceString(A, "B``)
"B` - 000 - B - 111 - "B` - 222 - B - 333 - "B` - 444 - B - 555 - "B` - 666 - B - 777 - "B` - 888 - B - 999 - "B`
ReplaceString(B, "A``) ;Bs between " and ` will not be replaced
"B` - 000 - "A`` - 111 - "B` - 222 - "A`` - 333 - "B` - 444 - "A`` - 555 - "B` - 666 - "A`` - 777 - "B` - 888 - "A`` - 999 - "B`
stringreplace, text, text, ", , all
stringreplace, text, text, ``, , all
B - 000 - A - 111 - B - 222 - A - 333 - B - 444 - A - 555 - B - 666 - A - 777 - B - 888 - A - 999 - B ;final result
Anyone?
.
works great, but uses "gosub" instead of "function"
Code: Select all
;RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;replace only a given string occurrence! (only first occurrence or only second occurrence, and so on!)
;(from "Pulover" AutoHotKey Forum User! Thanks!)
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16897
Text = A - 000 - B - 111 - A - 222 - B - 333 - A - 444 - B - 555 - A - 666 - B - 777 - A - 888 - B - 999 - A
n = 1 ;necessary for first "String1" occurrence
String1 = A
String2 = "B``
gosub, ReplaceString
String1 = B
String2 = "A``
gosub, ReplaceString
stringreplace, Text, Text, ``, , all
stringreplace, Text, Text, ", , all
msgbox, %Text%
return
ReplaceString:
loop,
{
Pos := InStr(Text, String1, true, , n) ;if "n =1" will seach first "String1" occurrence position, if n=2 search for second "String1" occurrence position and so on
;"true" casesensitive = true / omit or use "false" for casesensitive = false
if Pos = 0 ;means that "String1" for "n" occurrence doesn't exist, so position = 0
{ ;means that no "String1" occurrence found or no more "String1" occurence found
n = 1
break
}
loop,
{
if a_index = %Pos% ;(Pos-a_index = 0) means that " and ` were not found, so search to the left of String1 should stop
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
FindLeft := SubStr(Text, Pos-a_index, 1) ;find " or ` from the left of String1
;"1" only the first character after "Pos-a_index" position will be compared to " or `
if (FindLeft = """") or (FindLeft = "``") ;"" , first " treats the second " as a literal character
{ ;``, first ` treats second ` as a literal character
if FindLeft = ``
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
else
{
n++ ; example: if String1 first occurrence "n=1" can't be replaced, so String1 second occurrence "n=2" will be analysed instead, and so on
break
}
}
}
} ;end loop
return
A - 000 - B - 111 - A - 222 - B - 333 - A - 444 - B - 555 - A - 666 - B - 777 - A - 888 - B - 999 - A
String1 = A
String2 = "B``
gosub, ReplaceString
"B` - 000 - B - 111 - "B` - 222 - B - 333 - "B` - 444 - B - 555 - "B` - 666 - B - 777 - "B` - 888 - B - 999 - "B`
String1 = B
String2 = "A``
gosub, ReplaceString ;Bs between " and ` will not be replaced
"B` - 000 - "A`` - 111 - "B` - 222 - "A`` - 333 - "B` - 444 - "A`` - 555 - "B` - 666 - "A`` - 777 - "B` - 888 - "A`` - 999 - "B`
stringreplace, text, text, ", , all
stringreplace, text, text, ``, , all
B - 000 - A - 111 - B - 222 - A - 333 - B - 444 - A - 555 - B - 666 - A - 777 - B - 888 - A - 999 - B ;final result
with "gosub" works great, but an easier workaround would be welcome!!!
_______________________________________________________________
I tried to use a function (but didn't work):
Code: Select all
;RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;replace only a given string occurrence! (only first occurrence or only second occurrence, and so on!)
;(from "Pulover" AutoHotKey Forum User! Thanks!)
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16897
Text = A - 000 - B - 111 - A - 222 - B - 333 - A - 444 - B - 555 - A - 666 - B - 777 - A - 888 - B - 999 - A
n = 1 ;necessary for first "String1" occurrence
ReplaceString(A, "B``)
ReplaceString(B, "A``)
stringreplace, Text, Text, ``, , all
stringreplace, Text, Text, ", , all
msgbox, %Text%
return
ReplaceString(String1, String2)
{
loop,
{
Pos := InStr(Text, String1, true, , n) ;if "n =1" will seach first "String1" occurrence position, if n=2 search for second "String1" occurrence position and so on
;"true" casesensitive = true / omit or use "false" for casesensitive = false
if Pos = 0 ;means that "String1" for "n" occurrence doesn't exist, so position = 0
{ ;means that no "String1" occurrence found or no more "String1" occurence found
n = 1
break
}
loop,
{
if a_index = %Pos% ;(Pos-a_index = 0) means that " and ` were not found, so search to the left of String1 should stop
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
FindLeft := SubStr(Text, Pos-a_index, 1) ;find " or ` from the left of String1
;"1" only the first character after "Pos-a_index" position will be compared to " or `
if (FindLeft = """") or (FindLeft = "``") ;"" , first " treats the second " as a literal character
{ ;``, first ` treats second ` as a literal character
if FindLeft = ``
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
else
{
n++ ; example: if String1 first occurrence "n=1" can't be replaced, so String1 second occurrence "n=2" will be analysed instead, and so on
break
}
}
}
} ;end loop
} ;end function
A - 000 - B - 111 - A - 222 - B - 333 - A - 444 - B - 555 - A - 666 - B - 777 - A - 888 - B - 999 - A
ReplaceString(A, "B``)
"B` - 000 - B - 111 - "B` - 222 - B - 333 - "B` - 444 - B - 555 - "B` - 666 - B - 777 - "B` - 888 - B - 999 - "B`
ReplaceString(B, "A``) ;Bs between " and ` will not be replaced
"B` - 000 - "A`` - 111 - "B` - 222 - "A`` - 333 - "B` - 444 - "A`` - 555 - "B` - 666 - "A`` - 777 - "B` - 888 - "A`` - 999 - "B`
stringreplace, text, text, ", , all
stringreplace, text, text, ``, , all
B - 000 - A - 111 - B - 222 - A - 333 - B - 444 - A - 555 - B - 666 - A - 777 - B - 888 - A - 999 - B ;final result
Anyone?
.
Last edited by silvex3000 on 26 Apr 2016, 19:08, edited 1 time in total.
-
- Posts: 20
- Joined: 11 Sep 2015, 21:00
Re: (ALMOST SOLVED) StringReplace - Don't replace strings already replaced?
is everything you are analyzing the same character length (one) ? split the string into an array, analyze each character individually, and build a new string based of each character? i can see how trying to morph the same string over and over might make things tricky -altho i dont doubt there is a clever solution around. if you confirm the character length part i could provide some code.
- edit, Xtra actually already provided that solution sry
- edit, Xtra actually already provided that solution sry
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (ALMOST SOLVED) StringReplace - Don't replace strings already replaced?
@timelizardstimelizards wrote:is everything you are analyzing the same character length (one) ? split the string into an array, analyze each character individually, and build a new string based of each character? i can see how trying to morph the same string over and over might make things tricky -altho i dont doubt there is a clever solution around. if you confirm the character length part i could provide some code.
no, characters length vary
here's an example (the code below works great, but it uses "gosub" instead "function"):
Code: Select all
;RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;replace only a given string occurrence! (only first occurrence or only second occurrence, and so on!)
;(from "Pulover" AutoHotKey Forum User! Thanks!)
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16897
Text = CDBA
n = 1 ;necessary for first "String1" occurrence
String1 = C
String2 = "AAAA``
gosub, ReplaceString
String1 = D
String2 = "BBBB``
gosub, ReplaceString
String1 = A
String2 = "B``
gosub, ReplaceString
String1 = B
String2 = "A``
gosub, ReplaceString
stringreplace, Text, Text, ``, , all
stringreplace, Text, Text, ", , all
msgbox, %Text%
return
ReplaceString:
loop,
{
Pos := InStr(Text, String1, true, , n) ;if "n =1" will seach first "String1" occurrence position, if n=2 search for second "String1" occurrence position and so on
;"true" casesensitive = true / omit or use "false" for casesensitive = false
if Pos = 0 ;means that "String1" for "n" occurrence doesn't exist, so position = 0
{ ;means that no "String1" occurrence found or no more "String1" occurence found
n = 1
break
}
loop,
{
if a_index = %Pos% ;(Pos-a_index = 0) means that " and ` were not found, so search to the left of String1 should stop
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
FindLeft := SubStr(Text, Pos-a_index, 1) ;find " or ` from the left of String1
;"1" only the first character after "Pos-a_index" position will be compared to " or `
if (FindLeft = """") or (FindLeft = "``") ;"" , first " treats the second " as a literal character
{ ;``, first ` treats second ` as a literal character
if FindLeft = ``
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
else
{
n++ ; example: if String1 first occurrence "n=1" can't be replaced, so String1 second occurrence "n=2" will be analysed instead, and so on
break
}
}
}
} ;end loop
return
Text = CDBA
String1 = C
String2 = "AAAA``
gosub, ReplaceString
Text = "AAAA``DBA
String1 = D
String2 = "BBBB``
gosub, ReplaceString
Text = "AAAA``"BBBB``BA
String1 = B
String2 = "A``
gosub, ReplaceString ;Bs between " and `will not be replaced
Text = "AAAA``"BBBB``"A``A
String1 = A
String2 = "B``
gosub, ReplaceString ;As between " and `will not be replaced
Text = "AAAA``"BBBB``"A``"B``
stringreplace, Text, Text, ``, , all
stringreplace, Text, Text, ", , all
Text = AAAABBBBAB
______________________________________________
I want to use the code as a "function" instead using "gosub" (I tried but didn't work!)
instead using,
String1 = C
String2 = "AAAA``
gosub, ReplaceString
I would like to use,
ReplaceString(C, "AAAA``)
Anyone?
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (ALMOST SOLVED) StringReplace - Prevent replacing strings already replaced!
Another example:
Text = CCCDDDDBA
String1 = CCC
String2 = "A``
gosub, ReplaceString
Text = "A``DDDDBA
String1 = DDDD
String2 = "BB``
gosub, ReplaceString
Text = "A``"BB``BA
String1 = B
String2 = "A``
gosub, ReplaceString ;Bs between " and `will not be replaced
Text = "A``"BB``"A``A
String1 = A
String2 = "B``
gosub, ReplaceString ;As between " and `will not be replaced
Text = "A``"BB``"A``"B``
stringreplace, Text, Text, ``, , all
stringreplace, Text, Text, ", , all
Text = ABBAB ;final result
Code: Select all
;RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;replace only a given string occurrence! (only first occurrence or only second occurrence, and so on!)
;(from "Pulover" AutoHotKey Forum User! Thanks!)
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16897
Text = CCCDDDDBA
n = 1 ;necessary for first "String1" occurrence
String1 = CCC
String2 = "A``
gosub, ReplaceString
String1 = DDDD
String2 = "BB``
gosub, ReplaceString
String1 = A
String2 = "B``
gosub, ReplaceString
String1 = B
String2 = "A``
gosub, ReplaceString
stringreplace, Text, Text, ``, , all
stringreplace, Text, Text, ", , all
msgbox, %Text%
return
ReplaceString:
loop,
{
Pos := InStr(Text, String1, true, , n) ;if "n =1" will seach first "String1" occurrence position, if n=2 search for second "String1" occurrence position and so on
;"true" casesensitive = true / omit or use "false" for casesensitive = false
if Pos = 0 ;means that "String1" for "n" occurrence doesn't exist, so position = 0
{ ;means that no "String1" occurrence found or no more "String1" occurence found
n = 1
break
}
loop,
{
if a_index = %Pos% ;(Pos-a_index = 0) means that " and ` were not found, so search to the left of String1 should stop
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
FindLeft := SubStr(Text, Pos-a_index, 1) ;find " or ` from the left of String1
;"1" only the first character after "Pos-a_index" position will be compared to " or `
if (FindLeft = """") or (FindLeft = "``") ;"" , first " treats the second " as a literal character
{ ;``, first ` treats second ` as a literal character
if FindLeft = ``
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
else
{
n++ ; example: if String1 first occurrence "n=1" can't be replaced, so String1 second occurrence "n=2" will be analysed instead, and so on
break
}
}
}
} ;end loop
return
String1 = CCC
String2 = "A``
gosub, ReplaceString
Text = "A``DDDDBA
String1 = DDDD
String2 = "BB``
gosub, ReplaceString
Text = "A``"BB``BA
String1 = B
String2 = "A``
gosub, ReplaceString ;Bs between " and `will not be replaced
Text = "A``"BB``"A``A
String1 = A
String2 = "B``
gosub, ReplaceString ;As between " and `will not be replaced
Text = "A``"BB``"A``"B``
stringreplace, Text, Text, ``, , all
stringreplace, Text, Text, ", , all
Text = ABBAB ;final result
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (ALMOST SOLVED) StringReplace - Prevent replacing strings already replaced!
Coding functions is really tricky (But I finally made it)!
Works like a charm!
For those who helped, Thanks!
(An easier solution would be welcome!)
___________(strings between " and ` will not be replaced!)__________
UserText = CCCDDDBACCCDDD
UserText := ReplaceString(UserText, "CCC", """A``")
UserText = "A`DDDBA"A`DDD
UserText := ReplaceString(UserText, "DDD", """B``")
UserText = "A`"B`BA"A`"B`
UserText := ReplaceString(UserText, "B", """AAA``")
UserText = "A`"B`"AAA`A"A`"B`
UserText := ReplaceString(UserText, "A", """BBB``")
UserText = "A`"B`"AAA`"BBB`"A`"B`
stringreplace, UserText, UserText, ``, , all
stringreplace, UserText, UserText, ", , all
UserText = ABAAABBBAB ;final output
Works like a charm!
For those who helped, Thanks!
(An easier solution would be welcome!)
Code: Select all
;RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;replace only a given string occurrence! (only first occurrence or only second occurrence, and so on!)
;(from "Pulover" AutoHotKey Forum User! Thanks!)
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16897
UserText = CCCDDDBACCCDDD
UserText := ReplaceString(UserText, "CCC", """A``") ;"", first " treats second " as a literal character
UserText := ReplaceString(UserText, "DDD", """B``") ;``, first ` treats second ` as a literal character
UserText := ReplaceString(UserText, "B", """AAA``")
UserText := ReplaceString(UserText, "A", """BBB``")
stringreplace, UserText, UserText, ``, , all
stringreplace, UserText, UserText, ", , all
msgbox, %UserText%
return
ReplaceString(Text, String1, String2)
{
n = 1 ;necessary for first "String1" occurrence
loop,
{
Pos := InStr(Text, String1, true, , n) ;if "n =1" will seach first "String1" occurrence position, if n=2 search for second "String1" occurrence position and so on
;"true" casesensitive = true / omit or use "false" for casesensitive = false
if Pos = 0 ;means that "String1" for "n" occurrence doesn't exist, so position = 0
{ ;means that no "String1" occurrence found or no more "String1" occurence found
n = 1
return, Text ;stores variable "Text" value from "ReplaceString()" function, to the variable "UserText" that is outside "ReplaceString()" function
;in this case, "UserText" called "ReplaceString()" function,
}
loop,
{
if a_index = %Pos% ;(Pos-a_index = 0) means that " and ` were not found, so search to the left of String1 should stop
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
FindLeft := SubStr(Text, Pos-a_index, 1) ;find " or ` from the left of String1
;"1" only the first character after "Pos-a_index" position will be compared to " or `
if (FindLeft = """") or (FindLeft = "``") ;"" , first " treats the second " as a literal character
{ ;``, first ` treats second ` as a literal character
if FindLeft = ``
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
else
{
n++ ; example: if String1 first occurrence "n=1" can't be replaced, so String1 second occurrence "n=2" will be analysed instead, and so on
break
}
}
}
} ;end loop
} ;function end
UserText = CCCDDDBACCCDDD
UserText := ReplaceString(UserText, "CCC", """A``")
UserText = "A`DDDBA"A`DDD
UserText := ReplaceString(UserText, "DDD", """B``")
UserText = "A`"B`BA"A`"B`
UserText := ReplaceString(UserText, "B", """AAA``")
UserText = "A`"B`"AAA`A"A`"B`
UserText := ReplaceString(UserText, "A", """BBB``")
UserText = "A`"B`"AAA`"BBB`"A`"B`
stringreplace, UserText, UserText, ``, , all
stringreplace, UserText, UserText, ", , all
UserText = ABAAABBBAB ;final output
- AlphaBravo
- Posts: 586
- Joined: 29 Sep 2013, 22:59
Re: (SOLVED) StringReplace - Prevent replacing strings already replaced!
Code: Select all
Text = ABCD
Text := ReplaceString(Text, "A", "B")
Text := ReplaceString(Text, "B", "A")
Text := ReplaceString(Text, "C", "D")
Text := ReplaceString(Text, "D", "C")
Text := ReplaceString(Text, "BADC", "ZXYW")
MsgBox % Text := ReplaceString(Text) ; call function one more time to remove temp tags "~!@#~"
;---------------------
UserText := ReplaceString(UserText, "CCC", "A")
UserText := ReplaceString(UserText, "DDD", "B")
UserText := ReplaceString(UserText, "B", "AAA")
UserText := ReplaceString(UserText, "A", "BBB")
MsgBox % UserText := ReplaceString(UserText) ; call function one more time to remove temp tags "~!@#~"
return
;---------------------
ReplaceString(Text, String1:="", String2:=""){
return String1 ? RegExReplace(text, "~!@#~.*?~!@#~(*SKIP)(*F)|\Q" String1 "\E", "~!@#~" String2 "~!@#~") : RegExReplace(text, "~!@#~")
}
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (SOLVED) StringReplace - Prevent replacing strings already replaced!
@AlphaBravoAlphaBravo wrote:Code: Select all
Text = ABCD Text := ReplaceString(Text, "A", "B") Text := ReplaceString(Text, "B", "A") Text := ReplaceString(Text, "C", "D") Text := ReplaceString(Text, "D", "C") Text := ReplaceString(Text, "BADC", "ZXYW") MsgBox % Text := ReplaceString(Text) ; call function one more time to remove temp tags "~!@#~" ;--------------------- UserText := ReplaceString(UserText, "CCC", "A") UserText := ReplaceString(UserText, "DDD", "B") UserText := ReplaceString(UserText, "B", "AAA") UserText := ReplaceString(UserText, "A", "BBB") MsgBox % UserText := ReplaceString(UserText) ; call function one more time to remove temp tags "~!@#~" return ;--------------------- ReplaceString(Text, String1:="", String2:=""){ return String1 ? RegExReplace(text, "~!@#~.*?~!@#~(*SKIP)(*F)|\Q" String1 "\E", "~!@#~" String2 "~!@#~") : RegExReplace(text, "~!@#~") }
Wow!!!
Was that magic or what?
Thank you, Mr. David Copperfield!
(Any known Limitations?)
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (ALMOST SOLVED) StringReplace - Prevent replacing strings already replaced!
@AlphaBravo
Testing your code, so far, Great!
One minor limitation found though:
Lets say the original text contains "~!@#~" strings,
Text = ~!@#~ B ~!@#~ B ~!@#~
Text := ReplaceString(Text, "B", "T")
Text := ReplaceString(Text)
Text = ~!@#~ T ~!@#~ T ~!@#~ ("~!@#~" strings should not be removed!)
- Is it possible to keep "~!@#~" strings from "Text" variable?
- another problem is that, since the 2 "B" from original Text are enclosed by "~!@#~", they will not be replaced to "T"
Testing your code, so far, Great!
One minor limitation found though:
Lets say the original text contains "~!@#~" strings,
Text = ~!@#~ B ~!@#~ B ~!@#~
Text := ReplaceString(Text, "B", "T")
Text := ReplaceString(Text)
Text = ~!@#~ T ~!@#~ T ~!@#~ ("~!@#~" strings should not be removed!)
- Is it possible to keep "~!@#~" strings from "Text" variable?
- another problem is that, since the 2 "B" from original Text are enclosed by "~!@#~", they will not be replaced to "T"
- AlphaBravo
- Posts: 586
- Joined: 29 Sep 2013, 22:59
Re: (ALMOST SOLVED) StringReplace - Prevent replacing strings already replaced!
it is the same limitation as in your code except in mine I used random and not likely used sequence of characters "~!@#~", but the point here is to mark/tag replaced strings to prevent them from being replaced again, so whatever you choose as your mark/tag is going to be a problem if part of original text.(strings between " and ` will not be replaced!)
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (ALMOST SOLVED) StringReplace - Prevent replacing strings already replaced!
@AlphaBravoAlphaBravo wrote:it is the same limitation as in your code except in mine I used random and not likely used sequence of characters "~!@#~", but the point here is to mark/tag replaced strings to prevent them from being replaced again, so whatever you choose as your mark/tag is going to be a problem if part of original text.(strings between " and ` will not be replaced!)
@Pulover
Thank you, Sir!
here's another thing,
Do you know how to replace a string at a given position? (not at a given occurrence!)
if you know how, I would like you to post the solution in this thread:
https://autohotkey.com/boards/viewtopic.php?f=5&t=17236
Thanks!
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (ALMOST SOLVED) StringReplace - Prevent replacing strings already replaced!
@AlphaBravoAlphaBravo wrote: it is the same limitation as in your code except in mine I used random and not likely used sequence of characters "~!@#~", but the point here is to mark/tag replaced strings to prevent them from being replaced again, so whatever you choose as your mark/tag is going to be a problem if part of original text.
Are you really really sure that there is no way to keep "mark/tag" from "original text" by using "RegExReplace"?
with my function, I use only "<" and ">" as delimiters (mark/tag), and if original text contains delimiters (mark/tag), the function keeps them after replacements!
The only problem is that my function is getting a little bit long and I wanted something more "compact" and easier!
here is the code:
Code: Select all
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16819
;RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;replace only a given string occurrence! (only first occurrence or only second occurrence, and so on!)
;(from "Pulover" AutoHotKey Forum User! Thanks!)
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16897
;RegExReplace(RegExReplace(Text, "<(?!>)"), "<>", "<") ;replace strings with conditions
;from "AlphaBravo" Autohotkey forum user! Thanks!
;https://autohotkey.com/boards/viewtopic.php?p=84226#p84226
;< and > are used as Delimiters!
;ReplaceString(UserText, """", "G") ;"", first " treats second " as a literal character (just a note)
;ReplaceString(UserText, "``", "@") ;``, first ` treats second ` as a literal character (just a note)
UserText =
(
CCCDDDBA"""""""""""""""
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
CCCDDDBA"""""""""""""""
)
UserText := ReplaceString(UserText, "CCC", "A", "s") ;"s" means start string replacement of a given text
UserText := ReplaceString(UserText, "DDD", "B")
UserText := ReplaceString(UserText, "B", "AAA")
UserText := ReplaceString(UserText, "A", "BBB")
UserText := ReplaceString(UserText, """", "G")
UserText := ReplaceString(UserText, "``", "@")
UserText := ReplaceString(UserText, "G", "BBB", "e") ;"e" means end string replacement of a given text
gui, add, edit, r20 w400, %UserText%
gui, show
return
guiclose:
exitapp
ReplaceString(Text, String1, String2, Status = "", DelimiterL = "<", DelimiterR = ">") ; (Status = "") "status" default value is "blank" if user doesn't define it through function parameters
{
String2 = %DelimiterL%%String2%%DelimiterR% ;example: replace "String2 = B" to "String2 = <B>" (Add <> delimeters to String2)
if status = s ;"s" means start string replacement of a given text
{
stringreplace, Text, Text, >, >>, all ;> to >> must be first than <to <>
stringreplace, Text, Text, <, <>, all
}
n = 1 ;necessary for first "String1" occurrence
loop,
{
Pos := InStr(Text, String1, true, , n) ;if "n =1" will seach first "String1" occurrence position, if n=2 search for second "String1" occurrence position and so on
;"true" casesensitive = true / omit or use "false" for casesensitive = false
if Pos = 0 ;means that "String1" for "n" occurrence doesn't exist, so position = 0
{ ;means that no "String1" occurrence found or no more "String1" occurence found
n = 1
break
}
loop,
{
if a_index = %Pos% ;(Pos-a_index = 0) means that " and ` were not found, so search to the left of String1 should stop
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
FindLeft := SubStr(Text, Pos-a_index, 1) ;find < or > from the left of String1
;"1" only the first character after "Pos-a_index" position will be compared to < or >
if (FindLeft = "<") or (FindLeft = ">") ;"" , first " treats the second " as a literal character
{ ;``, first ` treats second ` as a literal character
if FindLeft = >
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
else
{
n++ ; example: if String1 first occurrence "n=1" can't be replaced, so String1 second occurrence "n=2" will be analysed instead, and so on
break
}
}
}
} ;end loop
if status = e ;"e" means end string replacement of a given text
{
Text := RegExReplace(RegExReplace(Text, "<(?!>)"), "<>", "<") ;"<(?!>)" first replace any "<" to "blank" if right of "<" is not ">" (remove "<" delimeters added to original text)
;("<>", "<") second replace any "<>" to "<" (keeps "<" from original text)
Text := RegExReplace(RegExReplace(Text, ">(?!>)"), ">>", ">") ;">(?!>)" first replace any ">" to "blank" if right of ">" is not ">" (remove ">" delimeters added to original text)
;(">>", ">") second replace any ">>" to ">" (keeps ">" from original text)
}
return, Text ;stores variable "Text" value from "ReplaceString()" function, to the variable "UserText" that is outside "ReplaceString()" function
;in this case, "UserText" called "ReplaceString()" function,
} ;function end
(
CCCDDDBA"""""""""""""""
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
CCCDDDBA"""""""""""""""
)
UserText := ReplaceString(UserText, "CCC", "A", "s") ;"s" means start string replacement of a given text
UserText := ReplaceString(UserText, "DDD", "B")
UserText := ReplaceString(UserText, "B", "AAA")
UserText := ReplaceString(UserText, "A", "BBB")
UserText := ReplaceString(UserText, """", "G")
UserText := ReplaceString(UserText, "``", "@")
UserText := ReplaceString(UserText, "G", "BBB", "e") ;"e" means end string replacement of a given text
Text =
(
ABAAABBBGGGGGGGGGGGGGGG
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
ABAAABBBGGGGGGGGGGGGGGG
)
As you can see, the function keeps (mark/tag) from original text!
Isn't it possible to do something similar by using "RegExReplace"?
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (ALMOST SOLVED) StringReplace - Prevent replacing strings already replaced!
"<" and ">" used as Delimiters! (Delimiters can be changed in function parameters to any other characters!)
Delimiters from original text, in this case "<" and ">", will not be removed from original text after replacements!
(Limitation) - "String1" or "String2" should not contain "Delimiters" characters!
Replacing Delimiters themselves or strings that contain Delimiters may or will certainly cause errors and unwanted final replacements output!
Example in this case: replace "<" to "AB" or replace "A>B" to "C" or replace "<<B" to "C<<D" (certainly will cause errors and unwanted results!)
if you want to replace "<" or ">" or strings that contain them from a given text, so change function delimiters parameter to something different, example, "7" and "8"!
example:
UserText =
(
<><><><><><>
AAAAAAAAAA
BBBBBBBBBBB
CCCCCCCCCC
DDDDDDDDDD
<><><><><><>
)
UserText := ReplaceString(UserText, "", "", "s") ;"s" start string replacement of a given text
UserText := ReplaceString(UserText, "A", "D")
UserText := ReplaceString(UserText, "B", "C")
UserText := ReplaceString(UserText, "C", "B")
UserText := ReplaceString(UserText, "D", "A")
UserText := ReplaceString(UserText, "", "", "e") ;"e" end string replacement of a given text
UserText =
(
<><><><><><>
DDDDDDDDDD
CCCCCCCCCCC
BBBBBBBBBB
AAAAAAAAAA
<><><><><><>
)
Delimiters from original text, in this case "<" and ">", will not be removed from original text after replacements!
(Limitation) - "String1" or "String2" should not contain "Delimiters" characters!
Replacing Delimiters themselves or strings that contain Delimiters may or will certainly cause errors and unwanted final replacements output!
Example in this case: replace "<" to "AB" or replace "A>B" to "C" or replace "<<B" to "C<<D" (certainly will cause errors and unwanted results!)
if you want to replace "<" or ">" or strings that contain them from a given text, so change function delimiters parameter to something different, example, "7" and "8"!
Code: Select all
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16819
;RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;replace only a given string occurrence! (only first occurrence or only second occurrence, and so on!)
;(from "Pulover" AutoHotKey Forum User! Thanks!)
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16897
;RegExReplace(RegExReplace(Text, "<(?!>)"), "<>", "<") ;replace strings with conditions
;from "AlphaBravo" Autohotkey forum user! Thanks!
;https://autohotkey.com/boards/viewtopic.php?p=84226#p84226
;< and > are used as Delimiters! (any character can be used as delimiter, example, 7 and 8)
;"DelimiterL" and "DelimiterR" should not be the same! (change Delimiters in the function parameters!)
;function parameters "String1" and "String2" should not contain "Delimiters" Character
;"String1" function parameter should not be "Blank"
;ReplaceString(UserText, """", "G") ;"", first " treats second " as a literal character (just a note)
;ReplaceString(UserText, "``", "@") ;``, first ` treats second ` as a literal character (just a note)
UserText =
(
CCCDDDBA"""""""""""""""
@@@@@@@@@@@@@@
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
8787878888888888887777777777777878788888888887777777777
8787778787877878787ggggg8787878778888877787878878878ttttttt
122890878787878787878787878788888887871212121223235678
@@@@@@@@@@@@@@
CCCDDDBA"""""""""""""""
)
UserText := ReplaceString(UserText, "", "", "s") ;"s" means start string replacement of a given text
;extra delimiters (<>) will be Added to the Original Text in order to prevent original delimiters (<>) from the original text to be removed after replacements!
UserText := ReplaceString(UserText, "CCC", "A")
UserText := ReplaceString(UserText, "DDD", "B")
UserText := ReplaceString(UserText, "B", "AAA")
UserText := ReplaceString(UserText, "A", "BBB")
UserText := ReplaceString(UserText, """", "G")
UserText := ReplaceString(UserText, "``", "@")
UserText := ReplaceString(UserText, "G", "BBB")
UserText := ReplaceString(UserText, "@", "") ;"" means that "String2 = blank", so, any "@" will be removed from text ("@" will be replaced to "blank")
;UserText := ReplaceString(UserText, "", "A") ;"String1 = Blank", warning message box will show up! ("String1" can't be "blank")
UserText := ReplaceString(UserText, "", "", "e") ;"e" means end string replacement of a given text
;remove all extra delimiters (<>) Added to the Original Text (original delimiters <> from the original text will not be removed after replacements!)
gui, add, edit, r20 w400, %UserText%
gui, show
return
guiclose:
exitapp
ReplaceString(Text, String1, String2, Status = "", DelimiterL = "<", DelimiterR = ">") ; (Status = "") "status" default value is "blank" if user doesn't define it through function parameters
{
If (InStr(string1, DelimiterL) || InStr(string1, DelimiterR) || InStr(string2, DelimiterL) || InStr(string2, DelimiterR)) ;if "string1" or "string2" contains "DelimiterR" or "DelimiterL"
msgbox, 0x42030 ,Warning!!!, (Replace "%String1%" to "%String2%") `n`n "String1" or "String2" contains "%DelimiterL%" or "%DelimiterR%" Delimiters! `n`n "String1" and "String2" should not contain "Delimiters"! `n`n Unwanted replacements or errors may occur! `n`n (Change Delimiters in the function parameters!)
if status = ;if status = "blank"
{
if String1 = ;if String1 = "Blank"
{
msgbox, 0x42030 ,Warning!!!,"String1" can't be "Blank": `n`n ReplaceString(Text, "%String1%", "%String2%") `n`n Replacement "Blank" to "%String2%" Denied! `n`n`n Press "OK" Button to Continue Replacements!
return, Text
}
}
if String2 != ;if String2 is different than "blank" (if String2 = "blank", delimiters <> should not be added to "String2", otherwise, some extra < delimiters will not be removed from the original text after replacements)
String2 = %DelimiterL%%String2%%DelimiterR% ;example: replace "String2 = B" to "String2 = <B>" (Add <> delimeters to String2)
;extra delimiters <> will be added to "String2" in order to prevent replacements of strings already replaced!
if status = s ;"s" means start string replacement of a given text
;extra delimiters (<>) will be Added to the Original Text in order to prevent original delimiters (<>) from the original text to be removed after replacements!
{
if DelimiterL = %DelimiterR%
msgbox, 0x42030 ,Warning!!!, "DelimiterL" and "DelimiterR" should not be the same! `n`n Unwanted replacements may occur! `n`n (Change Delimiters in the function parameters!)
stringreplace, Text, Text, %DelimiterR%, %DelimiterR%%DelimiterR%, all ;> to >> must be first than <to <>
stringreplace, Text, Text, %DelimiterL%, %DelimiterL%%DelimiterR%, all
return, Text ;stores variable "Text" value from "ReplaceString()" function, to the variable "UserText" that is outside "ReplaceString()" function
;in this case, "UserText" called "ReplaceString()" function,
}
if status = e ;"e" means end string replacement of a given text
;remove all extra delimiters (<>) Added to the Original Text (original delimiters <> from the original text will not be removed after replacements!)
{
RegTemp = %DelimiterL%(?!%DelimiterR%)
Text := RegExReplace(RegExReplace(Text, RegTemp), DelimiterL DelimiterR, DelimiterL) ;"<(?!>)" first replace any "<" to "blank" if right of "<" is not ">" (remove "<" delimeters added to original text)
;("<>", "<") second replace any "<>" to "<" (keeps "<" from original text)
RegTemp = %DelimiterR%(?!%DelimiterR%)
Text := RegExReplace(RegExReplace(Text, RegTemp), DelimiterR DelimiterR, DelimiterR) ;">(?!>)" first replace any ">" to "blank" if right of ">" is not ">" (remove ">" delimeters added to original text)
;(">>", ">") second replace any ">>" to ">" (keeps ">" from original text)
return, Text ;stores variable "Text" value from "ReplaceString()" function, to the variable "UserText" that is outside "ReplaceString()" function
;in this case, "UserText" called "ReplaceString()" function,
}
n = 1 ;necessary for first "String1" occurrence
loop,
{
Pos := InStr(Text, String1, true, , n) ;if "n =1" will seach first "String1" occurrence position, if n=2 search for second "String1" occurrence position and so on
;"true" casesensitive = true / omit or use "false" for casesensitive = false
if Pos = 0 ;means that "String1" for "n" occurrence doesn't exist, so position = 0
{ ;means that no "String1" occurrence found or no more "String1" occurence found
n = 1
return, Text ;stores variable "Text" value from "ReplaceString()" function, to the variable "UserText" that is outside "ReplaceString()" function
;in this case, "UserText" called "ReplaceString()" function,
}
loop,
{
if a_index = %Pos% ;(Pos-a_index = 0) means that " and ` were not found, so search to the left of String1 should stop
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
FindLeft := SubStr(Text, Pos-a_index, 1) ;find < or > from the left of String1
;"1" only the first character after "Pos-a_index" position will be compared to < or >
if (FindLeft = DelimiterL) or (FindLeft = DelimiterR) ;"" , first " treats the second " as a literal character
{ ;``, first ` treats second ` as a literal character
if FindLeft = %DelimiterR%
{
Text := RegExReplace(Text, String1, String2,, 1, InStr(Text, String1,,, n)) ;if "n=1" only the first "String1" occurrence will be replaced, if "n=2" only the second "String1" occurrence will be replaced
break
}
else
{
n++ ; example: if String1 first occurrence "n=1" can't be replaced, so String1 second occurrence "n=2" will be analysed instead, and so on
break
}
}
}
} ;end loop
} ;function end
UserText =
(
<><><><><><>
AAAAAAAAAA
BBBBBBBBBBB
CCCCCCCCCC
DDDDDDDDDD
<><><><><><>
)
UserText := ReplaceString(UserText, "", "", "s") ;"s" start string replacement of a given text
UserText := ReplaceString(UserText, "A", "D")
UserText := ReplaceString(UserText, "B", "C")
UserText := ReplaceString(UserText, "C", "B")
UserText := ReplaceString(UserText, "D", "A")
UserText := ReplaceString(UserText, "", "", "e") ;"e" end string replacement of a given text
UserText =
(
<><><><><><>
DDDDDDDDDD
CCCCCCCCCCC
BBBBBBBBBB
AAAAAAAAAA
<><><><><><>
)
Last edited by silvex3000 on 07 May 2016, 14:51, edited 1 time in total.
- silvex3000
- Posts: 188
- Joined: 19 Dec 2015, 22:42
Re: (SOLVED) StringReplace - Prevent replacing strings already replaced!
@AlphaBravo
I managed to implement your "RegExReplace" code in my function and now the function is a little bit shorter!
if you can improve the function, I would appreciate it!
UserText =
(
CCCDDDBA"""""""""""""""
@@@@@@@@@@@@@@
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
8787878888888888887777777777777878788888888887777777777
8787778787877878787ggggg8787878778888877787878878878ttttttt
122890878787878787878787878788888887871212121223235678
@@@@@@@@@@@@@@
<CCC<>DDD><BA"""" < """ > """"<<>>""> > < < "">
)
UserText := ReplaceString(UserText, "", "", "s") ;"s" start string replacement of a given text
UserText := ReplaceString(UserText, "CCC", "A")
UserText := ReplaceString(UserText, "DDD", "B")
UserText := ReplaceString(UserText, "B", "AAA")
UserText := ReplaceString(UserText, "A", "BBB")
UserText := ReplaceString(UserText, """", "G")
UserText := ReplaceString(UserText, "``", "@")
UserText := ReplaceString(UserText, "G", "BBB")
UserText := ReplaceString(UserText, "@", "")
UserText := ReplaceString(UserText, "", "", "e") ;"e" end string replacement of a given text
UserText =
(
ABAAABBBGGGGGGGGGGGGGGG
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
8787878888888888887777777777777878788888888887777777777
8787778787877878787ggggg8787878778888877787878878878ttttttt
122890878787878787878787878788888887871212121223235678
<A<>B><AAABBBGGGG < GGG > GGGG<<>>GG> > < < GG>
)
I managed to implement your "RegExReplace" code in my function and now the function is a little bit shorter!
if you can improve the function, I would appreciate it!
Code: Select all
;https://autohotkey.com/boards/viewtopic.php?f=5&t=16819
;RegExReplace(Text, RegTemp String1 "\E", String2) ;replace "String1" to "String2" if "String1" is not enclosed by delimiters!
;from "AlphaBravo" Autohotkey forum user!
;https://autohotkey.com/boards/viewtopic.php?p=83689#p83689
;RegExReplace(RegExReplace(Text, "<(?!>)"), "<>", "<") ;replace strings with conditions
;from "AlphaBravo" Autohotkey forum user!
;https://autohotkey.com/boards/viewtopic.php?p=84226#p84226
;< and > are used as Delimiters! (any character can be used as delimiter, example, 7 and 8)
;"DelimiterL" and "DelimiterR" should not be the same! (change Delimiters in the function parameters!)
;function parameters "String1" and "String2" should not contain "Delimiters" Character
;ReplaceString(UserText, """", "G") ;"", first " treats second " as a literal character (just a note)
;ReplaceString(UserText, "``", "@") ;``, first ` treats second ` as a literal character (just a note)
UserText =
(
CCCDDDBA"""""""""""""""
@@@@@@@@@@@@@@
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
8787878888888888887777777777777878788888888887777777777
8787778787877878787ggggg8787878778888877787878878878ttttttt
122890878787878787878787878788888887871212121223235678
@@@@@@@@@@@@@@
<CCC<>DDD><BA"""" < """ > """"<<>>""> > < < "">
)
UserText := ReplaceString(UserText, "", "", "s") ;"s" means start string replacement of a given text
;extra delimiters (<>) will be Added to the Original Text in order to prevent original delimiters (<>) from the original text to be removed after replacements!
UserText := ReplaceString(UserText, "CCC", "A")
UserText := ReplaceString(UserText, "DDD", "B")
UserText := ReplaceString(UserText, "B", "AAA")
UserText := ReplaceString(UserText, "A", "BBB")
UserText := ReplaceString(UserText, """", "G")
UserText := ReplaceString(UserText, "``", "@")
UserText := ReplaceString(UserText, "G", "BBB")
UserText := ReplaceString(UserText, "@", "") ;"" means that "String2 = blank", so, any "@" will be removed from text ("@" will be replaced to "blank")
;UserText := ReplaceString(UserText, "", "A") ;"String1 = Blank", warning message box will show up! ("String1" can't be "blank")
UserText := ReplaceString(UserText, "", "", "e") ;"e" means end string replacement of a given text
;remove all extra delimiters (<>) Added to the Original Text (original delimiters <> from the original text will not be removed after replacements!)
gui, add, edit, r20 w400, %UserText%
gui, show
return
guiclose:
exitapp
ReplaceString(Text, String1, String2, Status = "", DelimiterL = "<", DelimiterR = ">") ; (Status = "") "status" default value is "blank" if user doesn't define it through function parameters
{
If (InStr(string1, DelimiterL) || InStr(string1, DelimiterR) || InStr(string2, DelimiterL) || InStr(string2, DelimiterR)) ;if "string1" or "string2" contains "DelimiterR" or "DelimiterL"
msgbox, 0x42030 ,Warning!!!, (Replace "%String1%" to "%String2%") `n`n "String1" or "String2" contains "%DelimiterL%" or "%DelimiterR%" Delimiters! `n`n "String1" and "String2" should not contain "Delimiters"! `n`n Unwanted replacements or errors may occur! `n`n (Change Delimiters in the function parameters!)
if String2 != ;if String2 is different than "blank" (if String2 = "blank", delimiters <> should not be added to "String2", otherwise, some extra < delimiters will not be removed from the original text after replacements)
String2 = %DelimiterL%%String2%%DelimiterR% ;example: replace "String2 = B" to "String2 = <B>" (Add <> delimeters to String2)
;extra delimiters <> will be added to "String2" in order to prevent replacements of strings already replaced!
if status = s ;"s" means start string replacement of a given text
;extra delimiters (<>) will be Added to the Original Text in order to prevent original delimiters (<>) from the original text to be removed after replacements!
{
if DelimiterL = %DelimiterR%
msgbox, 0x42030 ,Warning!!!, "DelimiterL" and "DelimiterR" should not be the same! `n`n Unwanted replacements may occur! `n`n (Change Delimiters in the function parameters!)
stringreplace, Text, Text, %DelimiterR%, %DelimiterR%%DelimiterR%, all ;> to >> must be first than <to <>
stringreplace, Text, Text, %DelimiterL%, %DelimiterL%%DelimiterR%, all
return, Text ;stores variable "Text" value from "ReplaceString()" function, to the variable "UserText" that is outside "ReplaceString()" function
;in this case, "UserText" called "ReplaceString()" function,
}
if status = e ;"e" means end string replacement of a given text
;remove all extra delimiters (<>) Added to the Original Text (original delimiters <> from the original text will not be removed after replacements!)
{
RegTemp = %DelimiterL%(?!%DelimiterR%)
Text := RegExReplace(RegExReplace(Text, RegTemp), DelimiterL DelimiterR, DelimiterL) ;"<(?!>)" first replace any "<" to "blank" if right of "<" is not ">" (remove "<" delimeters added to original text)
;("<>", "<") second replace any "<>" to "<" (keeps "<" from original text)
RegTemp = %DelimiterR%(?!%DelimiterR%)
Text := RegExReplace(RegExReplace(Text, RegTemp), DelimiterR DelimiterR, DelimiterR) ;">(?!>)" first replace any ">" to "blank" if right of ">" is not ">" (remove ">" delimeters added to original text)
;(">>", ">") second replace any ">>" to ">" (keeps ">" from original text)
return, Text ;stores variable "Text" value from "ReplaceString()" function, to the variable "UserText" that is outside "ReplaceString()" function
;in this case, "UserText" called "ReplaceString()" function,
}
RegTemp = %DelimiterL%.*?%DelimiterR%(*SKIP)(*F)|\Q ;"<.*?>(*SKIP)(*F)|\Q"
return, RegExReplace(Text, RegTemp String1 "\E", String2) ;replace "String1" to "String2" if "String1" is not enclosed by delimiters!
}
(
CCCDDDBA"""""""""""""""
@@@@@@@@@@@@@@
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
8787878888888888887777777777777878788888888887777777777
8787778787877878787ggggg8787878778888877787878878878ttttttt
122890878787878787878787878788888887871212121223235678
@@@@@@@@@@@@@@
<CCC<>DDD><BA"""" < """ > """"<<>>""> > < < "">
)
UserText := ReplaceString(UserText, "", "", "s") ;"s" start string replacement of a given text
UserText := ReplaceString(UserText, "CCC", "A")
UserText := ReplaceString(UserText, "DDD", "B")
UserText := ReplaceString(UserText, "B", "AAA")
UserText := ReplaceString(UserText, "A", "BBB")
UserText := ReplaceString(UserText, """", "G")
UserText := ReplaceString(UserText, "``", "@")
UserText := ReplaceString(UserText, "G", "BBB")
UserText := ReplaceString(UserText, "@", "")
UserText := ReplaceString(UserText, "", "", "e") ;"e" end string replacement of a given text
UserText =
(
ABAAABBBGGGGGGGGGGGGGGG
12<<<>>>><><>>>>>>>1>2<23>>>>>><<<<5678><><><>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>ssssss>>>
<<<<<<zzzz>>>>>>>>><><><><>ggggg<><<<<<<<<<<>>>>>>>>>>
<<<<<<>>>>>>>>><><><><><><<<<<<<<<<>>>>>>>>>>
<><>><><ttttttt><<<<<<>>>>>>>>>>>>>><<<<<<<><122890>>
8787878888888888887777777777777878788888888887777777777
8787778787877878787ggggg8787878778888877787878878878ttttttt
122890878787878787878787878788888887871212121223235678
<A<>B><AAABBBGGGG < GGG > GGGG<<>>GG> > < < GG>
)