FrontPage  Index  Search  Changes  RSS  wikifarm  Login

正規表現Excelアドインソースコード

RegularExpressionMacro標準モジュール

Option Explicit

Dim re As New RegularExpression


Public Function RegExpReplace(ByVal 対象文字列 As String, ByVal 正規表現 As String, ByVal 置換文字列 As String, Optional ByVal 全体処理可能 As Boolean = True, Optional ByVal 大文字小文字区別無視 As Boolean = True, Optional ByVal 複数行文字列使用 As Boolean = True) As String
    With re
        Call .SetOption(全体処理可能, 大文字小文字区別無視, 複数行文字列使用)
        RegExpReplace = .replace(対象文字列, 正規表現, 置換文字列)
    End With
End Function

Public Function RegExpTest(ByVal 対象文字列 As String, ByVal 正規表現 As String, Optional ByVal 全体処理可能 As Boolean = True, Optional ByVal 大文字小文字区別無視 As Boolean = True, Optional ByVal 複数行文字列使用 As Boolean = True) As Boolean
    With re
        Call .SetOption(全体処理可能, 大文字小文字区別無視, 複数行文字列使用)
        RegExpTest = .test(対象文字列, 正規表現)
    End With
End Function

Public Function RegExpExecute(ByVal 対象文字列 As String, ByVal 正規表現 As String, ByVal 取得インデックス As Long, Optional ByVal 全体処理可能 As Boolean = True, Optional ByVal 大文字小文字区別無視 As Boolean = True, Optional ByVal 複数行文字列使用 As Boolean = True) As String
    With re
        Call .SetOption(全体処理可能, 大文字小文字区別無視, 複数行文字列使用)
        RegExpExecute = .Execute(対象文字列, 正規表現, 取得インデックス)
    End With
End Function

Public Function RegExpCount(ByVal 対象文字列 As String, ByVal 正規表現 As String, Optional ByVal 全体処理可能 As Boolean = True, Optional ByVal 大文字小文字区別無視 As Boolean = True, Optional ByVal 複数行文字列使用 As Boolean = True) As Long
    With re
        Call .SetOption(全体処理可能, 大文字小文字区別無視, 複数行文字列使用)
        RegExpCount = .count(対象文字列, 正規表現)
    End With
End Function

RegularExpressionクラスモジュール

Option Explicit

Private re As Object

Private Sub Class_Initialize()
    Set re = CreateObject("VBScript.RegExp")
End Sub

Private Sub Class_Terminate()
    Set re = Nothing
End Sub

Public Sub SetOption(ByVal isGlobal As Boolean, ByVal isIgnoreCase As Boolean, ByVal isMultiLine As Boolean)
    With re
        .Global = isGlobal
        .IgnoreCase = isIgnoreCase
        .MultiLine = isMultiLine
    End With
End Sub


Public Function replace(ByVal SourceString As String, ByVal Pattern As String, ByVal ReplaceString As String) As String
    With re
        .Pattern = Pattern
        replace = .replace(SourceString, ReplaceString)
    End With
End Function

Public Function test(ByVal SourceString As String, ByVal Pattern As String) As Boolean
    With re
        .Pattern = Pattern
        test = .test(SourceString)
    End With
End Function

Public Function Execute(ByVal SourceString As String, ByVal Pattern As String, ByVal Index As Long) As String
    With re
        .Pattern = Pattern
        Execute = .Execute(SourceString)(Index - 1)
    End With
End Function

Public Function count(ByVal SourceString As String, ByVal Pattern As String) As Long
    With re
        .Pattern = Pattern
        count = .Execute(SourceString).count
    End With
End Function
Last modified:2005/04/23 12:38:17
Keyword(s):
References: