您的位置:首页 > 编程语言 > VB

用vbs编写的将vbs代码中的Replace转换成Java的replace(第一版)

2012-06-27 01:03 393 查看
之前用vbs写过的一大段代码,现要用Java重写,工作量非常大,原想用正则表达式的方式去实现,但当代码中过于复杂时,正则表达式还是达不到效果,于是用vbs的土办法编写一个先将其中Replace转换成Java的replace,代码如下:

Function vbsToJava_Replace(content)
content = Replace(content, "replace", "Replace")

'替换Replace
startA = instr(content,"Replace(")
do while startA>0
findCount = 1
startA1 = startA + len("Replace(")
endA1 = startA
endA2 = startA
do while findCount>0 and (endA1 >0 or endA2>0)
endA1 = instr(startA1, content, "(")
do while mid(content, endA1 + 1, 1)="""" and mid(content, endA1 - 1, 1)=""""
endA1 = endA1 + 1
endA1 = instr(endA1, content, "(")
loop
endA2 = instr(startA1, content, ")")
do while mid(content, endA2 + 1, 1)="""" and  mid(content, endA2 - 1, 1)=""""
endA2 = endA2 + 1
endA2 = instr(endA2, content, ")")
loop
If (endA1>0 and endA2>0) Then
If (endA1<endA2) Then
findCount = findCount + 1
startA1 = endA1 + 1
Else
findCount = findCount - 1
startA1 = endA2 + 1
End If
ElseIf (endA1>0) Then
findCount = findCount + 1
startA1 = endA1 + 1
ElseIf (endA2>0) Then
findCount = findCount - 1
startA1 = endA2 + 1
End If
loop
endA = startA1
tmpStr1 = mid(content, startA, endA-startA)
Wscript.echo tmpStr1

tmpStr2 = tmpStr1
tmpStr3 = ""
startA2 = instr(tmpStr2, "Replace(")
do while startA2>0
preStr = ""
nextStr = ""
If startA2<>1 Then
preStr = Trim(mid(tmpStr2, 1, startA2 -1))
End If
startA2 = startA2 + len("Replace(")
endA2tmp = len(tmpStr2)
do
endA2 = instrRev(tmpStr2, ",", endA2tmp)
tmpStr2tmp = mid(tmpStr2, endA2, len(tmpStr2) - endA2)
yhCount = 0
yhHas = instr(tmpStr2tmp, """")
do while yhHas>0
yhCount = yhCount + 1
yhHas = instr(yhHas + 1, tmpStr2tmp, """")
loop
endA2tmp = endA2 - 1
loop while (yhCount mod 2 =1)

If (endA2>0) Then
do
endA2 = endA2 - 1
endA2 = instrRev(tmpStr2, ",", endA2)
tmpStr2tmp = mid(tmpStr2, endA2, len(tmpStr2) - endA2)
yhCount = 0
yhHas = instr(tmpStr2tmp, """")
do while yhHas>0
yhCount = yhCount + 1
yhHas = instr(yhHas + 1, tmpStr2tmp, """")
loop
loop while (yhCount mod 2 =1)
End If

If (len(preStr)>0) Then
If (endA2>0) Then
do
endA2 = endA2 - 1
endA2 = instrRev(tmpStr2, ",", endA2)
tmpStr2tmp = mid(tmpStr2, endA2, len(tmpStr2) - endA2)
yhCount = 0
yhHas = instr(tmpStr2tmp, """")
do while yhHas>0
yhCount = yhCount + 1
yhHas = instr(yhHas + 1, tmpStr2tmp, """")
loop
loop while (yhCount mod 2 =1)
End If
End If

If (endA2>0) Then
tmpStr21 = mid(tmpStr2, startA2, endA2 - startA2)
tmpStr3 = ".replace(" & mid(tmpStr2, endA2+1, len(tmpStr2)-endA2) & tmpStr3
tmpStr2 = preStr & tmpStr21
'Wscript.echo tmpStr3
End If
startA2 = instr(tmpStr2, "Replace(")
loop
tmpStr3 = tmpStr2 & tmpStr3
Wscript.echo tmpStr1 & vbcrlf & tmpStr3
content = Replace(content, tmpStr1, tmpStr3)
startA = instr(content,"Replace(")
loop
vbsToJava_Replace = content
End Function
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息