How do I get regex support in excel via a function, or custom function?

前端 未结 6 2178
有刺的猬
有刺的猬 2020-11-30 00:50

It appears that regex (as in regular expressions) is not supported in excel, except via VBA. Is this so, and if it is, are there any \"open source\" custom VBA functions tha

6条回答
  •  执笔经年
    2020-11-30 01:15

    I recently had the same exact question and after struggling with creating my own tools and making them work correctly I found a great online ADDIN that's very EASY to USE.

    This is the creator's excerpt

    For my internship over the past several months I’ve been working in the Marketing Science department and part of my job has been getting data into MS Access and generating reports. This involves getting lists of prospects from various data sources. This was usually a pretty simple feat involving some basic SQL queries. However, sometimes I was handed data such as addresses that didn’t match any standard format used by IT. In the worst case the data was provided in a pdf which meant I could only export it to a non-delimited text file. I found that I really needed a couple generic regular expression functions to parse out fields to import into MS Access. I found some .xla examples online but I really wanted an easier to use, more extensive, and portable library. I also wanted to include a few basic patterns for so it wasn’t necessary to re-invent the wheel every time.

    So, I created a simple Excel Add-In Regular Expressions.xla that adds several custom functions to implement the standard VBScript regular expressions.

    Here is the website

    I've used it successfully to extract useful text using regex.

    Here's the code in the addin

    ' Regular Expressions.xla
    '
    ' ? 2010 Malcolm Poindexter
    ' This is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
    ' as published by the Free Software Foundation, version 3.
    ' This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    ' without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    ' See the GNU General Public License for more details. http://www.gnu.org/licenses/gpl.html
    '
    ' I would appreciate if you would notify me of any modifications or re-distributions of this code at contact@malcolmp.com
    ' and appropriately attribute me as the original author in the header.
    ' ------------------------------------------------------------------------------------------------------------------------
    '
    ' This file provides an Excel Add-In containing regular expression processing functions and several pre-defined regular expressions.
    ' The regular expressions provided are not necessarially exhaustive, but are intended to cover the most common cases.
    '
    ' Regular Expressions Syntax: http://msdn.microsoft.com/en-us/library/1400241x%28VS.85%29.aspx
    
    ' -----------------------------
    ' NAME: xREPLACE
    ' DESCRIPTION: Replace all portions of the search text matching the pattern with the replacement text.
    ' -----------------------------
    Function xREPLACE(pattern As String, searchText As String, replacementText As String, Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = pattern
        RegEx.ignoreCase = ignoreCase
        xREPLACE = RegEx.Replace(searchText, replacementText)
    End Function
    
    ' -----------------------------
    ' NAME: xMATCHES
    ' DESCRIPTION: Find and return the number of matches to a pattern in the search text.
    ' -----------------------------
    Function xMATCHES(pattern As String, searchText As String, Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = pattern
        RegEx.ignoreCase = ignoreCase
        Dim matches As MatchCollection
        Set matches = RegEx.Execute(searchText)
        xMATCHES = matches.Count
    End Function
    
    ' -----------------------------
    ' NAME: xMATCH
    ' DESCRIPTION: Find and return an instance of a match to the pattern in the search text. MatchIndex may be used in the case of multiple matches.
    ' -----------------------------
    Function xMATCH(pattern As String, searchText As String, Optional matchIndex As Integer = 1, _
                    Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = pattern
        RegEx.ignoreCase = ignoreCase
        Dim matches As MatchCollection
        Set matches = RegEx.Execute(searchText)
        Dim i As Integer
        i = 1
        For Each Match In matches
            If i = matchIndex Then
                xMATCH = Match.Value
            End If
            i = i + 1
        Next
    End Function
    
    ' -----------------------------
    ' NAME: xMATCHALL
    ' DESCRIPTION: Find and return a comma-separated list of all matches to the pattern in the search text.
    ' -----------------------------
    Function xMATCHALL(pattern As String, searchText As String, Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = pattern
        RegEx.ignoreCase = ignoreCase
        Dim matches As MatchCollection
        Set matches = RegEx.Execute(searchText)
        Dim i As Integer
        i = 1
        Dim returnMatches As String
        returnMatches = ""
        For Each Match In matches
            If i = 1 Then
                returnMatches = Match.Value
            Else
                returnMatches = returnMatches + "," + Match.Value
            End If
            i = i + 1
        Next
        xMATCHALL = returnMatches
    End Function
    
    ' -----------------------------
    ' NAME: xGROUP
    ' DESCRIPTION: Find and return a group from within a matched pattern.
    ' -----------------------------
    Function xGROUP(pattern As String, searchText As String, group As Integer, Optional matchIndex As Integer = 1, _
                    Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        If group <> 0 Then
            group = group - 1
        End If
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = pattern
        RegEx.ignoreCase = ignoreCase
        Dim matches As MatchCollection
        Set matches = RegEx.Execute(searchText)
        Dim i As Integer
        i = 1
        For Each Match In matches
            If i = matchIndex Then
                xGROUP = Match.SubMatches(group)
            End If
            i = i + 1
        Next
    End Function
    
    ' -----------------------------
    ' NAME: xSTARTSWITH
    ' DESCRIPTION: Returns true or false if the search text starts with the pattern.
    ' -----------------------------
    Function xSTARTSWITH(pattern As String, searchText As String, Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = "^" + pattern
        RegEx.ignoreCase = ignoreCase
        Dim matches As MatchCollection
        Set matches = RegEx.Execute(searchText)
        xSTARTSWITH = matches.Count > 0
    End Function
    
    ' -----------------------------
    ' NAME: xENDSWITH
    ' DESCRIPTION: Returns true or false if the search text ends with the pattern.
    ' -----------------------------
    Function xENDSWITH(pattern As String, searchText As String, Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = pattern + "$"
        RegEx.ignoreCase = ignoreCase
        Dim matches As MatchCollection
        Set matches = RegEx.Execute(searchText)
        xENDSWITH = matches.Count > 0
    End Function
    
    ' ************************************
    ' Regular Expression Definitions
    ' ************************************
    
    ' -----------------------------
    ' NAME: xxEMAIL
    ' DESCRIPTION: Pattern to match an email address.
    ' -----------------------------
    Function xxEMAIL() As String
        xxEMAIL = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
    End Function
    
    ' -----------------------------
    ' NAME: xxUSZIP
    ' DESCRIPTION: Pattern to match an US Zip code.
    ' -----------------------------
    Function xxUSZIP() As String
        xxUSZIP = "\b(?!0{5})(\d{5})(?!-0{4})(-\d{4})?\b"
    End Function
    
    ' -----------------------------
    ' NAME: xxPHONE
    ' DESCRIPTION: Pattern to match a phone number.
    ' -----------------------------
    Function xxPHONE() As String
        xxPHONE = "\b[01]?[- .]?\(?[2-9]\d{2}\)?\s?[- .]?\s?\d{3}\s?[- .]?\s?\d{4}(\s*(x|(ext))[\.]?\s*\d{1,6})?\b"
    End Function
    
    ' -----------------------------
    ' NAME: xxURL
    ' DESCRIPTION: Pattern to match a url.
    ' -----------------------------
    Function xxURL() As String
        xxURL = "\b((ftp)|(https?))\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}\b"
    End Function
    
    
    ' ************************************
    '   Insert Function Dialog Category Setup
    ' ************************************
    Sub AddCategoryDescription()
        Application.MacroOptions Macro:="xREPLACE", _
            Description:="Replace all portions of the search text matching the pattern with the replacement text.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xMATCHES", _
            Description:="Find and return the number of matches to a pattern in the search text.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xMATCH", _
            Description:="Find and return an instance of a match to the pattern in the search text. MatchIndex may be used in the case of multiple matches.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xMATCHALL", _
            Description:="Find and return a comma-separated list of all matches to the pattern in the search text.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xGROUP", _
            Description:="Find and return a group from within a matched pattern.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xSTARTSWITH", _
            Description:="Returns true or false if the search text starts with the pattern.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xENDSWITH", _
            Description:="Returns true or false if the search text ends with the pattern.", _
            Category:="Regular Expressions"
    
        '**** Regular Expressions ****
    
        Application.MacroOptions Macro:="xxEMAIL", _
            Description:="Pattern to match an email address.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xxUSZIP", _
            Description:="Pattern to match an US Zip code.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xxPHONE", _
            Description:="Pattern to match a phone number.", _
            Category:="Regular Expressions"
    
        Application.MacroOptions Macro:="xxURL", _
            Description:="Pattern to match a url.", _
            Category:="Regular Expressions"
    End Sub
    

提交回复
热议问题