【发布时间】:2018-03-13 03:03:17
【问题描述】:
更新:2018 年 3 月 12 日下午 1:44 CST
创建一个名为http://vbfiddle.net 的网站以在浏览器中实施和测试@ctwheel 的VBScript 解决方案(MS IE 10+ 安全设置为“中”,该网站上的说明如何设置它以供您使用如果你想要 - 从 jsfiddle.net 的此链接获取代码以复制并粘贴到 vbfiddle.net:https://jsfiddle.net/hcwjhmg9/ [vbfiddle.net 目前没有“保存”功能]),我发现@ctwheel 的 VBScript RegEx 运行成功,即使是我给出的第三个示例行,但是当 @ctwheel 的 VBScript RegEx 用于 Microsoft Access 2016 的 VBA 的 VBScript 中针对从具有“相同”值的数据库读取的记录时,对于我给出的第三个示例行,第三个子组只返回“Ray”,它应该像在 vbfiddle.net 中一样返回“Ray, CFP”。
我终于想到遍历数据库返回的字符串值的每个字符(在 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”:“G.L. (Pete) Ray, CFP”。但是,如果我针对第 2 个直接输入代码的“2nd Text chars”示例运行 RegEx,则第 3 个子组返回“Ray,CFP”,如 Microsoft Access 2016 的 VBA 中所预期的那样。
我现在正在使用@ctwheels 提供的正则表达式:
^([^(]+?)\s*\(\s*([^)]*?)\s*\)\s*(.*)
有人能解释一下这里发生了什么吗? 1)为什么从数据库返回的字符与使用键盘通过视觉读取和复制输入字符串返回的字符不同? 2)当直接从数据库中读取值时,如何使适用于“1st Text Chars”字符/字符串序列的正则表达式返回正确的第三个子组:“Ray, CFP”?
原始问题(以上更新问题):
我在使用带有正则表达式引擎的 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).
【问题讨论】:
标签: regex vba debugging vbscript