0

I'm writing a compiler for my won computer language. Now before the language can be compiled i actually need to replace all apostrophes (') with percents (%) via a command-line vbs program. But the apostrophes only need to be replaced if there is NOT a circumflex accent (^) in front of it. So for example, in this code:

color 0a

input twelve = 0a "hi! that^'s great! "

execute :testfornum 'twelve'

exit

:testfornum

if numeric('1) (

return

) ELSE (

print 0a "oops 'twelve' should be numeric"

)

return

the apostrophe at line 2 should not be replaced, but the ones at line 3, 6 and 9 should be.

can anyone help me?

this is what i have so far:

'syntax: (cscript) replace.vbs [filename] "StringToFind" "stringToReplace"

Option Explicit
Dim FileScriptingObject, file, strReplace, strReplacement, fileD, lastContainment,        newContainment

file=Wscript.arguments(0)
strReplace=WScript.arguments(1)
strReplacement=WScript.arguments(2)

Set FileScriptingObject=CreateObject("Scripting.FileSystemObject")
if FileScriptingObject.FileExists(file) = false then
    wscript.echo "File not found!"
    wscript.Quit
end if

set fileD=fileScriptingobject.OpenTextFile(file,1)
lastContainment=fileD.ReadAll

newContainment=replace(lastContainment,strReplace,strReplacement,1,-1,0)
set fileD=fileScriptingobject.OpenTextFile(file,2)
fileD.Write newContainment
fileD.Close

2 Answers2

1

As @Ansgar's solution fails for the special case of a leading ' (no non-^ before that), here is an approach that uses a replace function in a test script that makes further experiments easy:

Option Explicit

Function fpR(m, g1, g2, p, s)
  If "" = g1 Then
     fpR = "%"
  Else
     fpR = m
  End If
End Function

Function qq(s)
  qq = """" & s & """"
End Function

Dim rE : Set rE = New RegExp
rE.Global = True
rE.Pattern = "(\^)?(')"
Dim rA : Set rA = New RegExp
rA.Global = True
rA.Pattern = "([^^])'"
'rA.Pattern = "([^^])?'"

Dim s
For Each s In Split(" 'a^'b' a'b'^'c nix a^''b")
    WScript.Echo qq(s), "==>", qq(rE.Replace(s, GetRef("fpR"))), "==>", qq(rA.Replace(s, "$1%"))
Next

output:

cscript 25221565.vbs
"" ==> "" ==> ""
"'a^'b'" ==> "%a^'b%" ==> "'a^'b%"     <=== oops
"a'b'^'c" ==> "a%b%^'c" ==> "a%b%^'c"
"nix" ==> "nix" ==> "nix"
"a^''b" ==> "a^'%b" ==> "a^'%b"
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • This particular edge case could be handled by adding "beginning of string" as an alternation to the regular expression: `(^|[^^])'` (I updated my answer accordingly). A replacement function would be too much hassle for just that. However, there's another edge case (sequential single quotes), which AFAICS does require a replacement function. It could be handled by capturing the sequence of single quotes in a second group `(^|[^^])('+)` and changing the function to something like this: `Function fpR(m, g1, g2, p, s) : fpR = g1 & Replace(g2, "'", "%") : End Function`. – Ansgar Wiechers Aug 09 '14 at 23:27
0

You can't do this with a normal string replacement. A regular expression would work, though:

...
Set re = New RegExp
re.Pattern = "(^|[^^])'"
re.Global  = True

newContainment = re.Replace(lastContainment, "$1%")
...
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328