特定的VBA / VBScript正则表达式相关问题

时间:2018-03-12 19:07:53

标签: regex vba debugging vbscript access-vba

更新时间:2018年3月12日中部标准时间下午1:44

原始问题位于此处:Specific VBA / VBScript regex related issue (MS Access)

以下是我对该问题的跟进。在此更新部分之后,原始问题也会被复制粘贴到此后续问题中(如果您想引用它):

创建一个名为http://vbfiddle.net的网站,在在线浏览器客户端(MS IE 10+,安全设置为“中等”)上实现并测试@ ctwheel的VBScript解决方案,该网站上的说明为如何设置它以供您使用 - 在jsfiddle.net上从此链接获取代码以复制并粘贴到vbfiddle.net中:https://jsfiddle.net/hcwjhmg9/ [ vbfiddle.net当前不可用有一个“保存”功能]),我发现@ ctwheel的VBScript RegEx运行成功,即使对于我给出的第三个示例行,但是当@ ctwheel的VBScript RegEx用于VBScript for VBA for Microsoft Access 2016时记录从具有“相同”值的数据库读取,第三个子组仅返回“Ray”,对于我给出的第3个示例行,它应该返回“Ray,CFP”,就像它在vbfiddle.net中一样。

我终于想到迭代数据库返回的字符串值的每个字符(在Microsoft Access中的VBA中),并将它与我直接输入的视觉等效字符串值的每个字符的迭代进行比较。代码(在Microsoft Access中的VBA中)。我得到以下结果:

First Name and Last Name: "G.L. (Pete) Ray, CFP"
--- 1st Text chars: "71 46 76 46 32 40 80 101 116 101 41 32 82 97 121 44"
(Read value from database, appears same as below when Debug.Print is called on it)
--- 2nd Text chars: "71 46 76 46 32 40 80 101 116 101 41 32 82 97 121 44 32 67 70 80" (Typed by keyboard into a string within the code)
'G.L. (Pete) Ray,'
    strProperName>objSubMatch: "G.L."
    strProperName>objSubMatch: "Pete"
    strProperName>objSubMatch: "Ray,"
 Matching record(s): 3 of 1132 record(s).

我正在运行的RegEx正在针对“1st Text Chars”示例运行,并返回“Ray”,用于先前给定的第3个示例行的第3个子组:“G.L. (Pete) Ray, CFP”。但是,如果我对第二个类型的RegEx运行 - 直接输入代码 - “2nd Text chars”示例,第三个子组将按照预期在VBA for Microsoft Access 2016中返回“Ray,CFP”。

我现在正在使用@ctwheels提供的RegEx:

^([^(]+?)\s*\(\s*([^)]*?)\s*\)\s*(.*)

有人可以解释这里发生了什么吗? 1)为什么从数据库返回的字符与使用键盘输入字符串返回的字符不同,通过直观地读取和复制它? 2)当如何直接从数据库中读取值时,如何使“1st Text Chars”字符/字符串序列的RegEx返回正确的第3个子组:“Ray, CFP”?

原始问题(上面更新的问题):

我在使用带有Regex引擎的Microsoft Access 2016的VBA中遇到问题我相信5.5 for VBScript。

这是我正在使用的正则表达式:

"(.*)\((.*)(\))(.*)"

我正在尝试解析字符串(分别在每个新行上):

Lawrence N. (Larry) Solomon
James ( Jim ) Alleman
G.L. (Pete) Ray, CFP

成:

"Lawrence N.", "Larry", ")", "Solomon"
"James", "Jim", ")", "Alleman"
"G.L.", "Pete", ")", "Ray, CFP"

或者(最好)进入:

"Lawrence N.", "Larry", "Solomon"
"James", "Jim", "Alleman"
"G.L.", "Pete", "Ray, CFP"

其中引号中的部分(以逗号分隔)是子匹配中返回的部分(不含引号)。

我使用以下代码:

' For Proper Name (strProperName):
With objRegex
    .Global = False
    .MultiLine = False
    .IgnoreCase = True
    .Pattern = "(.*)\((.*)(\))(.*)"

        '([\s|\S]*) work around to match every character?

            '".*\(([^\s]*)[\s]*\(" '_
            ''& "[\"
            '[\(][\s]*([.|^\w]*)[\s]*\)"
        ' "[\s]*(.*)[\s]*\("
            ' does same as below except matches any or no whitespace preceding any characters,
            ' and returns the following characters up to an opening parenthesis ("(") but excluding it,
            ' as the first subgroup
        ' "(.*)[\s]*\("
            ' does same as below except matches any whitespace or no whitespace at all followed by an opening parenthesis ("(")
            ' and returns the preceding characters as the first subgroup
        ' "(.*)\("
            ' matches all characters in a row that end with an open parenthesis, and returns all of these characters in a row
            ' excluding the following open parenthesis as the first subgroup
        ' "(.*?\(\s)"
        ' "[^\(]*"
            ' this pattern returns every character that isn't an opening parenthesis ("("), and when
            ' it matches an open parenthesis, it does not return it or any characters after it
        ' "[\(\s](.*)\)"
            ' this pattern extracts everything between parenthesis in a line as its first submatch
        ' "(?<=\().*"
        ' "[^[^\(]*][.*]"
        ' "(\(.*?\))"
        ' "(\(.*?\))*([^\(].*[^\)])"
End With

If objRegex.Test(strFirstNameTrimmed) Then
    'Set strsMatches = objRegex.Execute(rs.Fields("First Name"))
    Set strsMatches = objRegex.Execute(strFirstNameTrimmed)

    Debug.Print "2:'" & strsMatches(0).Value & "'"

    If strsMatches(0).SubMatches.Count > 0 Then
        For Each objSubMatch In strsMatches(0).SubMatches
            Debug.Print "    strProperName>objSubMatch: """ & objSubMatch & """" 'Result: 000, 643, 888"
            strProperName = objSubMatch
        Next objSubMatch
    End If
Else
    strProperName = "*Not Matched*"
End If

在调试窗口/“立即窗口”中生成以下输出,因为它在VBA中已知,由 Ctrl + G 生成:

------------------------
First Name and Last Name: "Lawrence N. (Larry) Solomon"
2:'Lawrence N. (Larry)'
    strProperName>objSubMatch: "Lawrence N. "
    strProperName>objSubMatch: "Larry"
    strProperName>objSubMatch: ")"
    strProperName>objSubMatch: ""
Extracted Nick Name: "Larry"
Extracted Proper Name: ""
First Name and Last Name: "James ( Jim ) Alleman"
2:'James ( Jim )'
    strProperName>objSubMatch: "James "
    strProperName>objSubMatch: " Jim "
    strProperName>objSubMatch: ")"
    strProperName>objSubMatch: ""
Extracted Nick Name: "Jim"
Extracted Proper Name: ""
First Name and Last Name: "G.L. (Pete) Ray, CFP"
2:'G.L. (Pete) Ray,'
    strProperName>objSubMatch: "G.L. "
    strProperName>objSubMatch: "Pete"
    strProperName>objSubMatch: ")"
    strProperName>objSubMatch: " Ray,"
Extracted Nick Name: "Pete"
Extracted Proper Name: " Ray,"
Matching record(s): 3 of 1132 record(s).

0 个答案:

没有答案