2012-03-06 3 views
1
나는 testscript에서 변수를 대체하기 위해 다음과 같은 방법을 사용

를 대체 할 값을 제외하는 방법, 대체 (간체) 다음 varreplace에서사용 정규식 일치 패턴 문자를 포함하지만

Dim myDict 
Set myDict = createobject("scripting.dictionary") 
mydict.add "subject", "testobject" 
mydict.add "name", "callsign" 
mydict.add "testobject callsign here", "Chell" 
mydict.add "subject hometown here", "City 17" 

Class cls_simpleReplacer 

    Private re_ 

    Public Sub Class_Initialize 
     Set re_ = new regexp 
     re_.global = true 
     re_.pattern = "\{[a-zA-Z][\w ]*\}" 
    End Sub 

    Public Function Parse(myString) 
     dim tStr 

     tStr = re_.replace(myString, getref("varreplace")) 

     ' see if there was a change 
     If tStr <> myString Then 
      ' see if we can replace more 
      tStr = Parse(tStr) 
     End If 

     Parse = tStr 
    End Function 

End Class 

Private Function varreplace(a, b, c) 
    varreplace = mydict.Item(mid(a, 2, len(a) - 2)) 
End Function 

MsgBox ((new cls_simpleReplacer).Parse("Unbelievable. You, {{subject} {name} here}, must be the pride of {subject hometown here}!")) 
' This will output `Unbelievable. You, Chell, must be the pride of City 17!`. 

, 나는 중괄호를 벗겨야한다 {}, 못생긴 (예를 들어, 이중 중괄호로 패턴을 바꿀 때, 나는 varreplace 함수를 변경해야한다)

패턴에 중괄호를 넣을 수는 있지만 그들을 포함하지 않는 방법이 있는가? 대체 문자열에? 변수 식별자의 형식과 길이에 신경 쓰지 않고 일반 varreplace 함수를 사용하고 싶습니다. ((일반) 그룹 캡처를 사용하여

Private Function varreplace(sMatch, sGroup1, nPos, sSrc) 
    varreplace = mydict(sGroup1) 
End Function 

는 중괄호 사이의 순서를 캡처 :

+0

문자열 앞뒤의 예는 무엇입니까? 이 함수의 입력 및 출력 예제는 무엇입니까? (소스?) 사전에 중괄호가 보이지 않습니다. 중괄호를 무시하거나 최소한 무시하는 것은 문제가되지 않아야한다. –

+0

코드를 다소 아래로 스크롤하면 (스크롤해야 볼 수있는 부분 바로 아래에 있음) 앞뒤 텍스트의 예가 표시됩니다. – AutomatedChaos

답변

1

는 {},

re_.pattern = "\{([a-zA-Z][\w ]*)\}" 

에 패턴을 변경하고 바꾸기 기능을 제거하려면) 함수에 매개 변수를 추가하면 사전 키를 즉시 액세스 할 수 있습니다.

+0

매력처럼 작동합니다! 고마워요! – AutomatedChaos