更新时间:2022-11-12 12:09:52
下面去与正则表达式的解决方案(完整的code这可以转换为功能):
子qTest_3()
昏暗objRE作为新的正则表达式
昏暗Tekst作为字符串
昏暗Wynik为Variant
Tekst =ODBC; DRIVER = SQL服务器;服务器= COMPNAME \ sqlex preSS; Trusted_Connection =是; APP =的Microsoft Office 2010; DATABASE =数据库名
随着objRE
。全球= TRUE
.IgnoreCase = TRUE
.Pattern =(^ *; SERVER =)(*)(;可信*)。
Wynik = .Replace(Tekst,$ 2)的模式只有第二部分将被退回
结束与
Debug.Print Wynik
结束小组
您的功能改变可能是如下(我添加了额外的参数设置的应返回的模式部分):
公共功能RxMatchReturn(_
BYVAL SourceString作为字符串_
BYVAL模式作为字符串_
StringPart为字节,_
可选BYVAL IGNORECASE由于布尔= TRUE,_
可选BYVAL多行由于布尔=真)为Variant
微软的VBScript普通防爆pressions 5.5
对错误转到errHandler
昏暗oMatches作为MatchCollection
随着新的正则表达式
.MultiLine =多行
.IgnoreCase = IGNORECASE
。全球= TRUE
.Pattern =模式
RxMatchReturn = .Replace(SourceString,$&安培; StringPart)
结束与
errHandler:
Debug.Print Err.Description它将
端功能
I am having a heck of a time with a RegEx question in Access VBA.
My goal is to extract the server from a linked database connection string. Basically, the connection string looks like
ODBC;DRIVER=SQL Server;SERVER=compName\sqlexpress;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=databaseName
I am able to get the first regex to work, but it is returning
SERVER=compName\sqlexpress
I would like this to only return
compName\sqlexpress
My understanding is the ?<=
operator should allow the RegEx to work correctly, but I get the following error "Method 'Execute' of object 'IRegExp2' failed."
The only documentation I can find for any Microsoft RegEx syntax is here which is not the runtime 5.5 VBScript library, but I'm not sure where else to get supported syntax.
Here is the code I'm using to test this. My database has a lot of linked tables.
Sub printServerStringInformation()
Dim rxPattern As String
rxPattern = "(?=SERVER)(.*)(?=;Trusted)"
Debug.Print RxMatch(CurrentDb.tableDefs(1).Connect, rxPattern, False)
rxPattern = "(?<=SERVER)(.*)(?=;Trusted)"
Debug.Print RxMatch(CurrentDb.tableDefs(1).Connect, rxPattern, False)
End Sub
Here is the function I am using:
Public Function RxMatch( _
ByVal SourceString As String, _
ByVal Pattern As String, _
Optional ByVal IgnoreCase As Boolean = True, _
Optional ByVal MultiLine As Boolean = True) As Variant
'Microsoft VBScript Regular Expressions 5.5
'http://www.zytrax.com/tech/web/regex.htm#more
'http://bytecomb.com/regular-expressions-in-vba/
'http://xkcd.com/1171/
On Error GoTo errHandler
Dim oMatches As MatchCollection
With New RegExp
.MultiLine = MultiLine
.IgnoreCase = IgnoreCase
.Global = False
.Pattern = Pattern
Set oMatches = .Execute(SourceString)
If oMatches.Count > 0 Then
RxMatch = oMatches(0).value
Else
RxMatch = ""
End If
End With
errHandler:
Debug.Print Err.Description
End Function
Here goes solution with RegEx (complete code which could be converted into function):
Sub qTest_3()
Dim objRE As New RegExp
Dim Tekst As String
Dim Wynik As Variant
Tekst = "ODBC;DRIVER=SQL Server;SERVER=compName\sqlexpress;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=databaseName"
With objRE
.Global = True
.IgnoreCase = True
.Pattern = "(^.*;SERVER=)(.*)(;Trusted.*)"
Wynik = .Replace(Tekst, "$2") 'only 2nd part of the pattern will be returned
End With
Debug.Print Wynik
End Sub
Your function changed could be as follows (I added additional parameter setting part of the pattern which should be returned):
Public Function RxMatchReturn( _
ByVal SourceString As String, _
ByVal Pattern As String, _
StringPart As Byte, _
Optional ByVal IgnoreCase As Boolean = True, _
Optional ByVal MultiLine As Boolean = True) As Variant
'Microsoft VBScript Regular Expressions 5.5
On Error GoTo errHandler
Dim oMatches As MatchCollection
With New RegExp
.MultiLine = MultiLine
.IgnoreCase = IgnoreCase
.Global = True
.Pattern = Pattern
RxMatchReturn = .Replace(SourceString, "$" & StringPart)
End With
errHandler:
Debug.Print err.Description
End Function