我将用示例代码解释我所追求的。我的功能GetDox
看起来很接近,但它仍然不完整。这是一个测试代码。
'test begin...
'<dox>
' <member type="Public Sub" name="Increment" return="void">
' <param type="Integer" name="nBase" out="true" />
' <param type="Integer" name="nStep" out="false" />
' <purpose>
' purpose here...
' </purpose>
' </member>
' <member ... />
'</dox>
'other comments here...
Public Sub Increment(nBase, nStep) 'some example content
nBase = nBase + nStep
End Sub
'<Unwonted_Item />
Dim source 'reading the same file just for simplification
With CreateObject("Scripting.FileSystemObject")
With .OpenTextFile(WScript.ScriptFullName, 1, False)
source = .ReadAll
End With
End With
result = GetDox(source)
WScript.Echo result 'display our result
Function GetDox(sCode) 'unfinished function
Dim regEx, Match, Matches, mVal, sEnd
sEnd = "</dox>" & vbNewLine
Set regEx = New RegExp
regEx.Pattern = "('<dox>\n|'\s*<.*)" 'my ugly pattern
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(sCode)
For Each Match In Matches
mVal = Match.Value
mVal = Replace(mVal, vbCr, vbNewLine)
mVal = Right(mVal, Len(mVal) - 1)
GetDox = GetDox & mVal
If mVal = sEnd Then Exit For
Next
End Function
这就是我得到的:
<dox>
<member type="Public Sub" name="Increment" return="void">
<param type="Integer" name="nBase" out="true" />
<param type="Integer" name="nStep" out="false" />
<purpose>
</purpose>
</member>
<member ... />
</dox>
这就是我需要的:
<dox>
<member type="Public Sub" name="Increment" return="void">
<param type="Integer" name="nBase" out="true" />
<param type="Integer" name="nStep" out="false" />
<purpose>
purpose here...
</purpose>
</member>
<member ... />
</dox>
缺少“这里的目的......”这一行,我知道整个RegExp.Pattern
语法很弱。我只想选择<dox>
以包含所有内容开头和结尾的整个内容</dox>
,但我坚持使用模式语法。
PS 有了如此出色的帮助(感谢所有人),这是我现在的工作职能:
Function GetDox(sCode)
GetDox = vbNullString
With New RegExp
.Pattern = "<dox>[\s\S]*?</dox>"
.IgnoreCase = True
.Global = False
With .Execute(sCode)
If .Count = 0 Then Exit Function
GetDox = .Item(0).Value
End With
.Pattern = "^'"
.Global = True
.Multiline = True
GetDox = .Replace(GetDox, "")
End With
End Function