If you make this into a sexy RegExp then I'll send you a surprise in
snailmail wherever you are in the world !
Thanks in advance,
Tom
I use the RegEx-object in VB6 and want it to to following.
Public Function MergeStr( _
ByVal sMask As String _
, ByVal sValues As String _
, Optional sMaskStr As String = vbNullString _
, Optional eFlags As TddStrMergeFlags = 0 _
) As String
sMask - The string to replace items in
sValues - A string containing alle the characters to replace
sMaskStr - The string to search for
eFlags - Enum that allows for case-support and REVERSE searching...
The spesial thing about this function is the reverse possibility and the
replacementstring sValues. Only the same number of characters is to be
replaced for each hit of sMaskStr in sMask from sValues:
Here are examples of wanted output:
stemp = objStreng.MergeStr("Telephone: ######## Fax: ########",
"6249085462490879", "#", esmIgnoreCase)
' -> sTemp = "Telephone: 62490854 Fax: 62490879"
stemp = objStreng.MergeStr("Telephone: ######## Fax: ########", "62490854",
"#")
' -> sTemp = "Telephone: 62490854"
stemp = objStreng.MergeStr("Telephone: ######## Fax: ",
"6249085462490879", "#")
' -> sTemp = "Telephone: 62490854 Fax: 62490879"
stemp = objStreng.MergeStr("Telephone: ######## Fax: ",
"6249085462490879", "")
' -> sTemp = "Telephone: ######## Fax: 6249085462490879"
stemp = objStreng.MergeStr("Country1: xxx Country2: xXx", "USANOR", "xxx")
' -> sTemp = "Country1: USA"
stemp = objStreng.MergeStr("Country1: xxx Country2: xXx", "USANOR", "xxx",
esmIgnoreCase)
' -> sTemp = "Country1: USA Country2: NOR"
stemp = objStreng.MergeStr("Country1: xxx Country2: xXx", "USANOR", "xxx",
esmIgnoreCase Or esmReverse)
' -> sTemp = "Country1: NOR Country2: USA"
Here is the code that I've made ( not taking all of the potensial in RegExp
that I think/hope possible??) and without the REVERSE possibilities...
Public Function MergeStr( _
ByVal sMask As String _
, ByVal sValues As String _
, Optional sMaskStr As String = vbNullString _
, Optional eFlags As TddStrMergeFlags = 0 _
) As String
On Error GoTo errhandler:
Dim objRegExp As RegExp, _
objMatch As Object, _
objMatches As Object, _
ix As Integer, _
strRet As String, _
strInsert As String, _
intLastPos As Integer, _
strMask As String
Set objRegExp = New RegExp
intLastPos = 1 'Starts at first position i string
objRegExp.IgnoreCase = (eFlags And esmIgnoreCase)
objRegExp.Global = True
objRegExp.Pattern = sMaskStr 'The reg.expr. is possibly here!
Set objMatches = objRegExp.Execute(sMask) ' Searches and returns a
collection of Match-classes
For Each objMatch In objMatches
strInsert = Mid$(sValues, ix * Len(sMaskStr) + 1, Len(sMaskStr))
If Len(strInsert) = 0 Then 'If there aint any more data to parse
Exit For
End If
'Adds the return string with the found hit replaced
strRet = strRet & Mid$(sMask, intLastPos, objMatch.FirstIndex -
intLastPos + 1) & strInsert
'Saves the last hit position to start the search here next time
intLastPos = objMatch.FirstIndex + Len(sMaskStr) + 1
ix = ix + 1
Next
'If not all merge data i replaced then I put it at the end of the result
string.
If Len(sValues) > (ix * Len(sMaskStr)) Then
strRet = strRet & Mid$(sMask, intLastPos) & Mid$(sValues, (ix *
Len(sMaskStr) + 1))
End If
MergeStr = strRet
Exit Function
errhandler:
MsgBox "An error in MergeStr." & vbNewLine & vbNewLine & "[" &
Err.Description & "]", vbCritical, "ddStringUtils"
End Function
>stemp = objStreng.MergeStr("Country1: xxx Country2: xXx", "USANOR", "xxx")
>' -> sTemp = "Country1: USA"
I was interested and considered playing with it and trying to maybe
make something work until I saw the above text.
Forget it, you're looking for Artificial Intelligence, not a string
manipulation function. How do you propose the function should know
where to cut off the output or why? The function should *know* that
since "xXx" doesn't match "xxx" that it is expected to trim off all of
" Country2: xXx" from the output?
What if I said ...
sTemp = objStreng.MergeStr("xxx Apples xXx Oranges, "001002", "xxx")
Should I expect back sTemp = "001 Apples" ? What if the first portion
of the string is bad but the end is good? Where do you draw the line
when you start letting the function "assume" for you ?
----------
Randy Hunt
Please let Microsoft know that we don't like the new MSDN:
http://register.microsoft.com/contactus30/feedback40.asp?FU=http%3A%2F%2Fmsdn%2Emicrosoft%2Ecom%2F&RU=http%3A%2F%2Fmsdn%2Emicrosoft%2Ecom%2Flibrary%2Fen%2Dus%2Fxmlsdk30%2Fhtm%2Fxmobjxmldomserverxmlhttp%5Fmultitiered%2Easp%3Fframe%3Dtrue&CU=http%3A%2F%2Fregister%2Emicrosoft%2Ecom%2Fcontactus30%2Fcontactus%2Easp%3Fdomain%3Dmsdn&Country=us&domain=msdnw
I'm pretty shure this must be accomplished with a mix between a replace
function in some way and a parse function after that. Sorry if this task is
to 'unnatural' to be solved in a quick way...
Tom
"Randy Hunt, MCSE" <ran...@wshscripting.com> wrote in message
news:3b4ed5fd....@news-server.tampabay.rr.com...