У меня недавно был тот же самый точный вопрос, и после того, как я попытался создать свои собственные инструменты и заставить их работать правильно, я нашел отличный онлайн ADDIN, который очень легко использовать.
Это выдержка создателя
В течение последних нескольких месяцев я проходил стажировку в отделе маркетинговых наук, и часть моей работы заключалась в получении данных в MS Access и создании отчетов.Это включает в себя получение списков потенциальных клиентов из различных источников данных.Обычно это был довольно простой подвиг, включающий некоторые базовые запросы SQL.Однако иногда мне передавали такие данные, как адреса, которые не соответствовали ни одному стандартному формату, используемому ИТ.В худшем случае данные были представлены в формате PDF, что означало, что я мог экспортировать их только в текстовый файл без разделителей.Я обнаружил, что мне действительно нужно несколько общих функций регулярного выражения для анализа полей для импорта в MS Access.В Интернете я нашел несколько примеров .xla, но я действительно хотел более простую в использовании, более обширную и переносимую библиотеку.Я также хотел включить несколько базовых шаблонов, чтобы не было необходимости каждый раз заново изобретать колесо.
Итак, я создал простую надстройку Excel Regular Expressions.xla, которая добавляет несколько пользовательских функций.для реализации стандартных регулярных выражений VBScript.
Вот сайт
Я успешно использовал его для извлечения полезного текста с помощью регулярных выражений.
Вот код в надстройке
' 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