Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Test_2()
- Dim oMatch As Object
- Dim iCount As Integer
- Dim oCell As Range
- Dim sPattern As String
- Dim sCapture1 As String
- Dim sCapture2 As String
- Set oCell = Application.ActiveCell
- sPattern = "([A-Z]*)(\d*)"
- iCount = SRE(oCell.Value, sPattern, oMatch, False, True, False) ' soll z.B. "C2" aber nicht "c2" treffen
- If iCount > 0 Then ' Ein Treffer ist aber immer vorhanden, auch wenn die Zelle leer ist!?!
- With oMatch.Item(0) ' Dann sind die Captures leer, das muss man also zusätzlich auswerten.
- sCapture1 = .SubMatches.Item(0) ' Ich hätte erwartet, dass nur bei Matches .Count > 0 ist ... seltsam
- sCapture2 = .SubMatches.Item(1)
- End With
- If sCapture1 <> "" And sCapture2 <> "" Then
- oCell.Value = sCapture1 & " == " & sCapture2
- End If
- End If
- Set oMatch = Nothing
- End Sub
- '-------------------------------------------------------------------------------------------------------------------------------
- ' Function Name: SRE
- ' Description: Checks a string with regular expression
- ' Parameter(s): sString String to check
- ' sPattern Test pattern
- ' oResult object to receive the result
- ' optional: fGlobal RegExp property .Global (default = False)
- ' optional: fMultiLine RegExp property .MultiLine (default = True)
- ' optional: fIgnoreCase RegExp property .IgnoreCase (default = True)
- ' Return Value(s): Count of matches ????
- '-------------------------------------------------------------------------------------------------------------------------------
- Function SRE(sString As Variant, sPattern As String, oResult As Object, Optional fGlobal As Boolean = False, Optional fMultiLine As Boolean = True, Optional fIgnoreCase As Boolean = True) As Integer
- Dim oRegEx As Object
- Set oRegEx = CreateObject("VBscript.RegExp")
- With oRegEx
- .Global = fGlobal
- .MultiLine = fMultiLine
- .IgnoreCase = fIgnoreCase
- .Pattern = sPattern
- Set oResult = .Execute(sString)
- End With
- Set oRegEx = Nothing
- SRE = oResult.Count
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement