Advertisement
BugFix

Untitled

Apr 26th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.21 KB | None | 0 0
  1. Sub Test_2()
  2.     Dim oMatch As Object
  3.     Dim iCount As Integer
  4.     Dim oCell As Range
  5.     Dim sPattern As String
  6.     Dim sCapture1 As String
  7.     Dim sCapture2 As String
  8.    
  9.     Set oCell = Application.ActiveCell
  10.     sPattern = "([A-Z]*)(\d*)"
  11.    
  12.     iCount = SRE(oCell.Value, sPattern, oMatch, False, True, False) ' soll z.B. "C2" aber nicht "c2" treffen
  13.      
  14.     If iCount > 0 Then                         ' Ein Treffer ist aber immer vorhanden, auch wenn die Zelle leer ist!?!
  15.         With oMatch.Item(0)                    ' Dann sind die Captures leer, das muss man also zusätzlich auswerten.
  16.             sCapture1 = .SubMatches.Item(0)    ' Ich hätte erwartet, dass nur bei Matches .Count > 0 ist ... seltsam
  17.             sCapture2 = .SubMatches.Item(1)
  18.         End With
  19.         If sCapture1 <> "" And sCapture2 <> "" Then
  20.             oCell.Value = sCapture1 & " == " & sCapture2
  21.         End If
  22.     End If
  23.    
  24.     Set oMatch = Nothing
  25. End Sub
  26.  
  27. '-------------------------------------------------------------------------------------------------------------------------------
  28. ' Function Name:   SRE
  29. ' Description:     Checks a string with regular expression
  30. ' Parameter(s):    sString      String to check
  31. '                  sPattern     Test pattern
  32. '                  oResult      object to receive the result
  33. '      optional:   fGlobal      RegExp property .Global     (default = False)
  34. '      optional:   fMultiLine   RegExp property .MultiLine  (default = True)
  35. '      optional:   fIgnoreCase  RegExp property .IgnoreCase (default = True)
  36. ' Return Value(s): Count of matches  ????
  37. '-------------------------------------------------------------------------------------------------------------------------------
  38. 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
  39.     Dim oRegEx As Object
  40.      
  41.     Set oRegEx = CreateObject("VBscript.RegExp")
  42.     With oRegEx
  43.         .Global = fGlobal
  44.         .MultiLine = fMultiLine
  45.         .IgnoreCase = fIgnoreCase
  46.         .Pattern = sPattern
  47.         Set oResult = .Execute(sString)
  48.     End With
  49.    
  50.     Set oRegEx = Nothing
  51.     SRE = oResult.Count
  52. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement