Hi dm_4ever and all other people willing to accept a challenge,
as to be expected one of the most stalwart contributors to this topic posted
code well worth studying. It won't be a surprise that after seeing the good points
of his approach, I feel inclined to switch to nit picking mode:
(1) My aim for challenge (a) was to have a single point/ad hoc way of using
RegExps in VBScript. In dm_4ever's code you have to look at 2-3 parts of
the code (definition of pattern, setting of vbTextCompare/IgnoreCase, and
execution) to understand what's going on. I had in mind something like this:
... code with no reference to the RegExp ...
If reTest( sTest, <suitable params 01> ) Then
sRes = reReplace( sTest, <suitable params 02> )
Else
sRes = reReplace( sTest, <suitable params 03> )
End If
I admit that using reTest() etc. in a loop - like dm_4ever's test code - isn't
the best context to see/demonstrate the usefulness of such functions, because
the RegExp would be created for each round. Obviously the object should be
created before entering such a loop. That's why I came up with challenge (c).
Given such a wrapper class, the instantiation/initialization could occur before
the loop and using the object within would be trivial
Dim oRE : Set oRE = New cRE.Init( <suitable params> ) ' all interesting facts
' about the RegExp visible
' here (perhaps including
' the replacement)
....
Do
sSrc = <whatever>
sRes = oRE.Replace( sSrc, sRpl )
Or even
sRes = oRE.Replace( sSrc )
Loop
(2) I don't think that the params to GetRegEx() are 'suitable'. It should be possible
to specify all properties of the resulting RegExp:
Global - IgnoreCase - Multiline - Pattern
Forcing Global to be True (just to be able to pull the (ingenious) trick of
applying the RegExp to its own pattern in CleanRegPattern()) won't do. I'm wondering,
whether looking at Perl's specifying such properties by letters/string - gimsx -
could be a way.
(3) For the 'cleaning' of the RegExp's pattern I'm experimenting with:
Set s_reFmtX = New RegExp
s_reFmtX.Pattern = "(?:\s)|#(?:[\D\d]+?)$"
s_reFmtX.Global = True
s_reFmtX.Multiline = True
...
m_oRE.Pattern = goReLib.s_reFmtX.Replace( sPattern, "" )
...
That presupposes using \x20 for space and \23 for # in the readable definition.
An example for a complex pattern used to cut text blocks from a longer string/
file like
...
BEGIN_CodeBlock VBSSimpleClass
''= §ClassName§ - §ClassPurpose§
' ============================================================================
Class §ClassName§
...
End Class ' §ClassName§
END_CodeBlock VBSSimpleClass
...
BEGIN_CodeBlock VBSXplFunc
' ============================================================================
goXPLLib.Add _
"§XplFuncName§", "§XplFuncPurpose§"
' ============================================================================
Function §XplFuncName§()
Dim nRVal : nRVal = 0
§XplFuncName§ = nRVal
End Function
'NextXplFunc
END_CodeBlock VBSXplFunc
...
Set s_reBlocks = New cRE.initPF( Join( Array( _
"^ # am Anfang der Zeile" _
, "BEGIN_ # konstanter erster Teil des Tag" _
, "(\w+) # \1: variabler zweiter Teil des Tag " _
, "\s+ # whitespace bis zum" _
, "(\w+) # \2: Namen des Blocks" _
, "\r\n # neue Zeile vor Block" _
, "([\D\d]+?) # $3: Block=WirklichAlles (aber nicht gierig)" _
, "\r\n # neue Zeile vor Block" _
, "^ # am Anfang der Zeile" _
, "END_ # konstanter erster Teil des Close Tag" _
, "\1 # \1: zweiter Teils des Close Tag wie oben" _
, "\s+ # whitespace bis zum" _
, "\2 # \2: Namen des Blocks" _
), vbLf ), "GiMX" )
Eagerly awaiting further solutions ...
ehvbs