title image


Smiley Regexp

Wie wäre es denn damit...

 

Option ExplicitSub Main()    Call Test_FormatRegExEnd Sub Sub Test_FormatRegEx()    Dim FormatString As String    Dim FormatSearchPattern As String    Dim FormatTargetPattern As String    Dim FormatLen As Long        Debug.Print "Debug Print Start"        FormatString = GetString("12345678", 2)    Debug.Print FormatString    FormatString = Replace(FormatString, "  ", "-")    FormatString = Replace(FormatString, " ", "-")    FormatString = Replace(FormatString, "--", "-")    Debug.Print FormatString    FormatLen = Len(Replace(FormatString, "-", ""))        FormatSearchPattern = "("        FormatLen = IIf(FormatLen Mod 2 <> 0, 3, 2)        Dim i As Long    For i = 1 To FormatLen        FormatSearchPattern = FormatSearchPattern & "[0-9]"        FormatTargetPattern = FormatTargetPattern & "$" & Trim(Str(i))        Next i        FormatSearchPattern = FormatSearchPattern & "?)"        Debug.Print FormatString, FormatSearchPattern, FormatTargetPattern            Debug.Print FormatRegEx(FormatString, FormatSearchPattern, "$4.$3.$2.$1")        Debug.Print FormatRegEx(FormatString, FormatSearchPattern, "$1.$2.$3.$4")        Debug.Print FormatRegEx(FormatString, FormatSearchPattern, "$2.$1.$4.$3")        Debug.Print "Debug Print Ende"        Debug.PrintEnd SubFunction GetString(Input1 As String, Input2 As Long) As String        Dim LokalString As String    Dim Counter As Long        LokalString = Trim(Input1)        If Len(LokalString) < Input2 Or Input2 < 1 Then        'Längenangabe passt nicht, dann String komplett zurückgeben        GetString = LokalString        Exit Function        ElseIf Len(LokalString) Mod Input2 <> 0 Then        'Stringlänge ist nicht ganzzahlig teilbar        If Input2 Mod 2 <> 0 Then            Counter = -1                    Else            Counter = 1                    End If        GetString = Left(LokalString, Input2 + Counter)        LokalString = Right(LokalString, Len(LokalString) - (Input2 + Counter))    End If    'Rest aufteilen    For Counter = 1 To Len(LokalString) Step Input2        GetString = GetString & Space(1) & Mid(LokalString, Counter, Input2)            Next Counter    GetString = Trim(GetString)    End FunctionFunction FormatRegEx(str_Input As String, str_Pattern As String, str_Zielpattern As String) As String    Dim regEx As New RegExp    Dim alleTreffer As MatchCollection    Dim str_Teile As String    Dim str_Ergebnis As String    Dim myMatchCT    str_Ergebnis = str_Zielpattern    If str_Pattern <> "" Then        With regEx            .Global = True            .MultiLine = True            .IgnoreCase = True            .Pattern = str_Pattern        End With        If regEx.Test(str_Input) Then            Set alleTreffer = regEx.Execute(str_Input)            If alleTreffer.Count <> 0 Then                With alleTreffer                    For myMatchCT = 0 To .Count - 1                        str_Teile = .Item(myMatchCT)                        regEx.Pattern = "\$" & myMatchCT + 1                        str_Ergebnis = regEx.Replace(str_Ergebnis, str_Teile)                    Next                End With            End If        End If    End If    FormatRegEx = str_ErgebnisEnd Function

 


Wer weiß schon, was er nicht weiß...(ich zähle mich auch dazu) Holger

geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: