View Single Post
Old 06-15-2009   #9 (permalink)
ekkehard.horner


 
 

Re: script for multi text replacement

tonycd schrieb:
[...]
Quote:

> But how can i enhance the script for multi - text replacement.
[...]
To make it easy to experiment with the aspects pointed out by
the other contributors (and to add an array/loop based approach):

demo script:

Dim sSrc : sSrc = "one two three ONE TWO THREE one two three"
Dim sTrg

' Replace( expression, find, replacewith [, start (1) [, count (-1) [, compare
(vbBinaryCompare) ) ] ] ] )

sTrg = Replace( sSrc, "one" , "eins" )
sTrg = Replace( sTrg, "two" , "zwei" )
sTrg = Replace( sTrg, "three", "drei" )
WScript.Echo Join( Array( "sequential repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = Replace( sSrc, "one" , "eins", 1, -1, vbTextCompare )
sTrg = Replace( sTrg, "two" , "zwei", 1, -1, vbTextCompare )
sTrg = Replace( sTrg, "three", "drei", 1, -1, vbTextCompare )
WScript.Echo Join( Array( "sequential repl, all, ignore case", sSrc, sTrg, "" ), vbCrLf )

sTrg = Replace( sSrc, "one" , "eins", 1, 1, vbBinaryCompare )
sTrg = Replace( sTrg, "two" , "zwei", 1, 1, vbBinaryCompare )
sTrg = Replace( sTrg, "three", "drei", 1, 1, vbBinaryCompare )
WScript.Echo Join( Array( "sequential repl, just one, ignore case", sSrc, sTrg, "" ),
vbCrLf )

sTrg = Replace( Replace( Replace( sSrc, "one", "eins" ), "two", "zwei" ), "three", "drei" )
WScript.Echo Join( Array( "nested repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = Replace( Replace( Replace( sSrc, "one", "eins", 1, 2, vbTextCompare ), "two",
"zwei", 1, 2, vbTextCompare ), "three", "drei", 1, 2, vbTextCompare )
WScript.Echo Join( Array( "nested repl, just two, ignore case", sSrc, sTrg, "" ), vbCrLf )

Dim aRpl( 1, 2 )
Dim nRpl
aRpl( 0, 0 ) = "one"
aRpl( 1, 0 ) = "eins"
aRpl( 0, 1 ) = "two"
aRpl( 1, 1 ) = "zwei"
aRpl( 0, 2 ) = "three"
aRpl( 1, 2 ) = "drei"

sTrg = sSrc
For nRpl = 0 To UBound( aRpl, 2 )
sTrg = Replace( sTrg, aRpl( 0, nRpl ), aRpl( 1, nRpl ) )
Next
WScript.Echo Join( Array( "looped repl, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = sSrc
For nRpl = 0 To UBound( aRpl, 2 )
sTrg = Replace( sTrg, aRpl( 0, nRpl ), aRpl( 1, nRpl ), 1, -1, vbTextCompare )
Next
WScript.Echo Join( Array( "looped repl, all, ignore case", sSrc, sTrg, "" ), vbCrLf )

sTrg = replaceAll( sSrc, aRpl )
WScript.Echo Join( Array( "repl func, all, honor case", sSrc, sTrg, "" ), vbCrLf )

sTrg = replaceAllP( sSrc, aRpl, 2, vbTextCompare )
WScript.Echo Join( Array( "repl func parm, just two, ignore case", sSrc, sTrg, "" ),
vbCrLf )

sTrg = Replace( sSrc, "one" , "eins", 5, -1, vbTextCompare )
WScript.Echo Join( Array( "one repl, all, ignore case, starting at 5", sSrc, sTrg, ""
), vbCrLf )

Function replaceAll( sSrc, aRpl )
replaceAll = sSrc
Dim nRpl
For nRpl = 0 To UBound( aRpl, 2 )
replaceAll = Replace( replaceAll, aRpl( 0, nRpl ), aRpl( 1, nRpl ) )
Next
End Function

Function replaceAllP( sSrc, aRpl, nCount, vbComp )
replaceAllP = sSrc
Dim nRpl
For nRpl = 0 To UBound( aRpl, 2 )
replaceAllP = Replace( replaceAllP, aRpl( 0, nRpl ), aRpl( 1, nRpl ), 1, nCount,
vbComp )
Next
End Function

output:

=== replDemo04: Replace Demo (04) =============================
sequential repl, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

sequential repl, all, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei eins zwei drei

sequential repl, just one, ignore case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE one two three

nested repl, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

nested repl, just two, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei one two three

looped repl, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

looped repl, all, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei eins zwei drei

repl func, all, honor case
one two three ONE TWO THREE one two three
eins zwei drei ONE TWO THREE eins zwei drei

repl func parm, just two, ignore case
one two three ONE TWO THREE one two three
eins zwei drei eins zwei drei one two three

one repl, all, ignore case, starting at 5
one two three ONE TWO THREE one two three
two three eins TWO THREE eins two three

=== replDemo04: 0 done (00:00:00) =============================

The last test case is included to show that using the start
parameter should be avoided in multi-text replacements.

My System SpecsSystem Spec