RegEx-Module

nefarius2001

Grünschnabel
Hallo allerseits,
eine kleine Frage - ich sollte dazu sagen dass es sich um Excel-Makros handelt.

Ich habe folgendes im Internet gefunden:

KLASSENMODUL RegEx
Code:
    ''' RegExp -- use VBScript RegExp object to provide regular expressions
    '
    '   2006-01-26  Olivier Boissin,        enhanced version (added Multiline property)
    '   2004-06-03  Davids Phillips, rfdinc.com First version.
    
    Public matches As Variant
    Public oRegExp As Variant
    
    ' VBScript RegExp properties
    Public pattern As String
    Public IgnoreCase As Boolean ' default = False
    Public SearchWholeString As Boolean ' default = False
    Public Multiline As Boolean ' default = False
    

    
    Public Function Match(source As String, pattern As String, matches As Variant) As Boolean
        ' RegEx.Match -- scan source for pattern, set matches collection and return true if any
        ' (Can't call it Execute as that collides with LotusScript built-in function and statement.)
        With oRegExp
            .pattern = pattern ' regular expression to match
            .IgnoreCase = IgnoreCase
            .Global = SearchWholeString
            .Multiline = Multiline
            Set matches = .Execute(source)  ' do match
            Match = (Not 0 = matches.Count)
        End With
    End Function
    
    Public Function Replaces(source As String, pattern As String, replacement As String) As String
        ' RegEx.Replaces -- scan source for pattern, if found substitute replacement, return result
        ' (Can't call it Replace as that collides with LotusScript built-in function.)
        With oRegExp
            .pattern = pattern
            .IgnoreCase = IgnoreCase
            .Global = SearchWholeString
            .Multiline = Multiline
            Replaces = .Replace(source, replacement)  ' do replace
        End With
    End Function
    
    Public Function Test(source As String, pattern As String) As Boolean
        ' RegEx.Test -- scan source for pattern, return true if found
        With oRegExp
            .pattern = pattern
            .IgnoreCase = IgnoreCase
            .Multiline = Multiline
            Test = .Test(source)
        End With
    End Function

Nur - ich hab noch nie diese Klassenmodule hergenommen, weil nie bedarf da war. Meine Version sieht so aus:
MODUL RegEx
Code:
Option Explicit

'HELP
'keep in mind: to Match the first Group, use "$1", not "\1"
'
'Gives back a MatchCollection
'There are: (complete)
'MatchCollection.Count  Number of matches
'MatchCollection.Item   Needs Index (X = 0 to Matches.Count - 1), default method is Value
'MatchCollection.Item.FirstIndex    Number of characters in the string to the left of the match => 0 if found at the very beginning
'MatchCollection.Item.Length        Number of characters in the match
'MatchCollection.Item.Value         Text matched
'MatchCollection.Item.SubMatches    only existent if Pattern contains Capturig Groups" "()"
'MatchCollection.Item.SubMatches.Count     Number of Capturing Groups used
'MatchCollection.Item.SubMatches.Item(X)   needs Index (X = 0 to SubMatches.Count - 1), returns captured text
'


'From http://www.regular-expressions.info/vbscript.html
'No \A or \Z anchors to match the start or end of the string. Use a caret or dollar instead.
'Lookbehind is not supported at all. Lookahead is fully supported.
'No atomic grouping or possessive quantifiers
'No Unicode support, except for matching single characters with \uFFFF
'No named capturing groups. Use numbered capturing groups instead.
'No mode modifiers to set matching options within the regular expression.
'No conditionals.
'No regular expression comments. Describe your regular expression with VBScript apostrophe comments instead, outside the regular expression string.



Public Function RegExp_Search( _
strSource As String, _
strSearchFor As String, _
varMatches As Variant, _
Optional NumberOfMatches As Integer, _
Optional RegExp_IgnoreCase As Boolean = False, _
Optional RegExp_OnlyFirstMatch As Boolean = False, _
Optional RegExp_Multiline As Boolean = True) _
As Boolean
Dim oRegExp As Object
Set oRegExp = CreateObject("VBScript.RegExp")
'Searching for Pattern in Source-String
'Giving Back:
' - the Number of Matches as Function
' - the Matches as varMatches-Array

' RegEx.Match -- scan source for pattern, set matches collection and return true if any
    With oRegExp
        .Pattern = strSearchFor ' regular expression to match
        .IgnoreCase = RegExp_IgnoreCase
        .Global = Not RegExp_OnlyFirstMatch
        .MultiLine = RegExp_Multiline
        Set varMatches = .Execute(strSource)  ' do match
        RegExp_Search = varMatches.Count
    End With
End Function

Public Function RegExp_Replace( _
strSource As String, _
strSearchFor As String, _
strReplacement As String, _
Optional NumberOfMatches As Integer, _
Optional RegExp_IgnoreCase As Boolean = False, _
Optional RegExp_OnlyFirstMatch As Boolean = False, _
Optional RegExp_Multiline As Boolean = True) _
As String
    
    ' RegEx.Replaces -- scan source for pattern, if found substitute replacement, return result
Dim oRegExp As Object
Dim varX
Set oRegExp = CreateObject("VBScript.RegExp")
    With oRegExp
        .Pattern = strSearchFor
        .IgnoreCase = RegExp_IgnoreCase
        .Global = Not RegExp_OnlyFirstMatch
        .MultiLine = RegExp_Multiline
         Set varX = .Execute(strSource)
        NumberOfMatches = NumberOfMatches + varX.Count
        RegExp_Replace = .Replace(strSource, strReplacement)  ' do replace
        
    End With
End Function

Public Function RegExp_AnyMatches( _
strSource As String, _
strSearchFor As String, _
Optional RegExp_IgnoreCase As Boolean = False, _
Optional RegExp_Multiline As Boolean = True) _
As Boolean
    
    ' RegEx.Test -- scan source for pattern, return true if found
Dim oRegExp As Object
Set oRegExp = CreateObject("VBScript.RegExp")
    With oRegExp
        .Pattern = strSearchFor
        .IgnoreCase = RegExp_IgnoreCase
        .MultiLine = RegExp_Multiline
        RegExp_AnyMatches = .test(strSource)
    End With
End Function

Nun die Frage in die Runde: was bringt das eine, was das andere?
Ich kann noch nicht mal sagen ob/dass das Klassenmodul funktioniert.
Das normale benutze ich in meiner Anwendung - es läuft einwandfrei.

Bin mal gespannt was ihr so dazu sagt.
 
Zurück