regex - 将正则表达式模式从子传递到 Excel VBA 中的函数

标签 regex function vba arguments

我正在尝试将 Regex 模式传递给 Excel VBA 中的函数,但该模式似乎无效。我已插入 msgbox'es 以查看字符串的外观,结果正常。
这是我正在使用的代码。

Sub clean_COP_names()
Dim strSheet As String
Dim strPatternOrig As String

Dim strRow As Integer
Dim strCol As Integer
Dim UpBound As Range
Dim LowBound As Range

Dim strUpBoundRow As Integer
Dim strUpBoundColumn As Integer
Dim strLowBoundRow As Integer
Dim strLowBoundColumn As Integer
Dim CompareRange As Range


Dim c As Variant
Dim d As Integer
    Dim strTest As String
    strTest = ActiveCell.Value

    strSheet = "Sheet2"

    strRow = 2
    strCol = 2
    strUpBoundRow = 0
    strUpBoundColumn = 0
    strLowBoundRow = 0
    strLowBoundColumn = 0

    '/////call ext function
    SelectColumn strSheet, strRow, strCol, strUpBoundRow, strUpBoundColumn, strLowBoundRow, strLowBoundColumn

    Set CompareRange = Worksheets(strSheet).Range _
(Cells(strUpBoundRow, strUpBoundColumn), Cells(strLowBoundRow, strLowBoundColumn))


    d = 1
    Cells(d, 6).Value = "Alumni Officer - Last,First names"
    strPatternOrig = """^([^ ]+)([ ]+)([^ ]+)([ ]+)([^ ]+)(.*)$"""
    'MsgBox (strPatternOrig)
    For Each c In CompareRange
    d = d + 1
        '/////ext function
        Cells(d, 6).Value = Reorder_Name_COP_Data_a(c.Value, strPatternOrig, "$3,$1")
    Next
End Sub


Function Reorder_Name_COP_Data_a(strData As String, strPattern As String, strReplacementPattern As String) As String

Dim RE As Object

Set RE = CreateObject("vbscript.regexp")
With RE
    .MultiLine = False
    '.Global = False
    .Global = True
    .IgnoreCase = True
    'MsgBox (strPattern)

    .Pattern = strPattern
End With

Reorder_Name_COP_Data_a = RE.Replace(strData, strReplacementPattern)

End Function

==================

增编 2012 年 4 月 26 日
非常感谢-

我注意到当我使用转义引号时问题仍然存在,如下所示:
 strPatternOrig = "^[ ]?([^\ ,()""'']+)(?:[ ](\(([^)]*?)\)))?[ ]((?:(([^\ ,()""''])[^\ ,()""'']*)[ ])([^\ ,()""'']+(?:[ ][^\ ,()""'']+)*))(?: [ ]? , [ ]?(.*?))?[ ]?(\(\s*'*\d*\s*\))[ ]?$"

双引号和单引号是否需要以不同的方式转义,可能吗?当 Regex 模式“硬连接”到函数中时,上面的方法有效,但是当它传递给函数时,它失败了。
再次感谢。

最佳答案

您不需要转义单引号,只需要转义双引号。一旦变量被分配了一个字符串常量,它就可以自由传递并且不会改变。

您在使用大正则表达式时遇到的唯一真正问题是它不匹配,因为您在其中留下了一些“空气”。
这是你所拥有的:

"^[ ]?([^\ ,()""'']+)(?:[ ](\(([^)]*?)\)))?[ ]((?:(([^\ ,()""''])[^\ ,()""'']*)[ ])([^\ ,()""'']+(?:[ ][^\ ,()""'']+)*))(?: [ ]? , [ ]?(.*?))?[ ]?(\(\s*'*\d*\s*\))[ ]?$"

这是应该的:
"^[ ]?([^\ ,()""']+)(?:[ ](\(([^)]*?)\)))?[ ]((?:(([^\ ,()""'])[^\ ,()""']*)[ ])([^\ ,()""']+(?:[ ][^\ ,()""']+)*))(?:[ ]?,[ ]?(.*?))?[ ]?(\(\s*'*\d*\s*\))[ ]?$"

这是您的正则表达式的测试用例(如果我记得的话,它只匹配多最后一个表单):
Dim RXE As Object
Dim RXNorm As Object

Sub RegexColumnValueComparison()
  Dim strData As String
  Dim strPat As String
  Call InitializeRXs

   ' Here, the grad part ('#) is optional
   strPat = "^[ ]?([^\ ,()""']+)(?:[ ](\(([^)]*?)\)))?[ ]((?:(([^\ ,()""'])[^\ ,()""']*)[ ])([^\ ,()""']+(?:[ ][^\ ,()""']+)*))(?:[ ]?,[ ]?(.*?))?[ ]?(?:(\(\s*'*\d*\s*\))[ ]?)?$"
   ' Here, the grad part ('#) is required
   'strPat = "^[ ]?([^\ ,()""']+)(?:[ ](\(([^)]*?)\)))?[ ]((?:(([^\ ,()""'])[^\ ,()""']*)[ ])([^\ ,()""']+(?:[ ][^\ ,()""']+)*))(?:[ ]?,[ ]?(.*?))?[ ]?(\(\s*'*\d*\s*\))[ ]?)$"

   strData = " John   Bert Smith, Jr  ('78) "
   MsgBox (RxRepl(strData, strPat, "$7 $8 , $1 $3 $6 $9"))
End Sub

Function RxRepl(sData As String, sPat As String, sRepl As String) As String
   sData = RXNorm.Replace(sData, " ")
   RXE.Pattern = sPat
     ' Can test for pass/fail ..
     'If RXE.Test(sData) Then
     '   MsgBox ("matched pattern")
     'Else
     '   MsgBox ("did NOT match pattern")
     'End If
   RxRepl = RXE.Replace(sData, sRepl)
End Function

Sub InitializeRXs()
  Set RXE = CreateObject("vbscript.regexp")
  Set RXNorm = CreateObject("vbscript.regexp")
  RXE.Global = True
  RXNorm.Global = True
  RXNorm.Pattern = "\s+"
End Sub

关于regex - 将正则表达式模式从子传递到 Excel VBA 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10324638/

相关文章:

matlab - 定义具有多个无法组织成矩阵的输出的函数

javascript - 如何让这个 Javascript 函数正确迭代?

vba - 如果在大型数据集上使用 Excel VBA 循环,则执行速度非常慢,然后崩溃

excel - 保护 Excel 工作表 - 不可能?

javascript - 正则表达式非将符号后的字符转换为大写

用于 Mark Nelson 实现 Ukkonen 算法的后缀树中首次出现模式 s 的 java 模式匹配

java - 使用 Java Pattern 和 Matcher 搜索包含正斜杠等标点符号的模式

java - 用其上层变体替换正则表达式捕获

c++ - 从函数返回对象时是否创建了临时对象?

vba - 确定使用ms-access的ODBC故障(错误3146)的真正原因?