gpt4 book ai didi

regex - 在 VBA 中使用正则表达式和正则表达式

转载 作者:行者123 更新时间:2023-12-04 21:58:30 35 4
gpt4 key购买 nike

这不是我完全编写的代码,有些是我从一两个站点拼凑而成的,有些是我设置的。我想要做的是使用 中定义的正则表达式函数。正则表达式模式 查看消息主题并提取一个值。这是我将在电子邮件主题中看到的内容:

新的 Linux 服务器:prod-servername-a001

到目前为止,我可以将完整的消息主题放入 Excel 文件中,但是当我尝试实现正则表达式部分时,我收到错误代码 5017(我可以找到的表达式错误)并且正则表达式不是“工作”。我的期望是脚本将提取消息主题,使用正则表达式提取值并将其放置在单元格中。我正在使用 RegEx Builder(正则表达式测试程序)来测试表达式,它在那里工作,但不是在这里。我对 VB 很陌生,所以我不知道问题是 VB 不能使用这个表达式,还是脚本在其他地方失败,而错误是另一个问题的残余。或者有没有更好的方法来写这个?

Sub ExportToExcel()
On Error GoTo ErrHandler

'Declarations
Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Excel.Range
Dim strSheet As String
Dim filePath As String
Dim strPath As String
Dim intRowCounter As Integer
Dim intColumnCounter As Integer
Dim msg As Outlook.MailItem
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itm As Object

'RegEx Declarations
Dim result As String
Dim allMatches As Object
Dim regex As Object
Set regex = CreateObject("vbscript.regexp")

regex.Pattern = "(?<=Server: ).*"
regex.Global = True
regex.IgnoreCase = True


' Set the filename and path for output, requires creating the path to work
strSheet = "outlook.xlsx"
strPath = "D:\temp\"
filePath = strPath & strSheet

'Debug
Debug.Print filePath

'Select export folder
Set nms = Application.GetNamespace("MAPI")
Set fld = nms.PickFolder

'Handle potential errors with Select Folder dialog box.
If fld Is Nothing Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub

ElseIf fld.DefaultItemType <> olMailItem Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub

ElseIf fld.Items.Count = 0 Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub
End If

'Open and activate Excel workbook.
Set appExcel = CreateObject("Excel.Application")
appExcel.Workbooks.Open (filePath)
Set wkb = appExcel.ActiveWorkbook
Set wks = wkb.Sheets(1)
wks.Activate
appExcel.Application.Visible = True


'Copy field items in mail folder.
For Each itm In fld.Items
intColumnCounter = 1
Set msg = itm

If itm.UnRead = True Then
intRowCounter = intRowCounter + 1
wks.Cells(1, 1).value = "Subject" 'Row 1 Column 1 (A)
wks.Cells(1, 2).value = "Unread" 'Row 1 Column 2 (B)
wks.Cells(1, 3).value = "Server" 'Row 1 Column 3 (C)

Set rng = wks.Cells(intRowCounter + 1, intColumnCounter)

If InStr(msg.Subject, "Server:") Then
Set allMatches = regex.Execute(msg.Subject)
rng.value = allMatches
intColumnCounter = intColumnCounter + 1
msg.UnRead = False

Else
rng.value = msg.Subject
intColumnCounter = intColumnCounter + 1
msg.UnRead = False
End If

Set rng = wks.Cells(intRowCounter + 1, intColumnCounter)
rng.value = msg.UnRead
intColumnCounter = intColumnCounter + 1
End If

Next itm
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
Exit Sub


ErrHandler:

If Err.Number = 1004 Then
MsgBox filePath & " doesn't exist", vbOKOnly, "Error"

ElseIf Err.Number = 13 Then
MsgBox Err.Number & ": Type Mismatch", vbOKOnly, "Error"
ElseIf Err.Number = 438 Then
MsgBox Err.Number & ": Object doesn't support this property or method", vbOKOnly, "Error"
ElseIf Err.Number = 5017 Then
MsgBox Err.Number & ": Error in expression", vbOKOnly, "Error"
Else
MsgBox Err.Number & ": Description: ", vbOKOnly, "Error"

End If


Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing

End Sub

enter image description here

最佳答案

VBA 正则表达式不支持lookbehinds,但在这种情况下,您不需要积极的lookbehind,您只需使用捕获组-“Server: (.*)”`-然后访问 Group 1 值:

Set regex = CreateObject("vbscript.regexp")
regex.Pattern = "Server: (.*)"
regex.IgnoreCase = True
Set allMatches = regex.Execute("New Linux Server: prod-servername-a001")
If allMatches.Count <> 0 Then
rng.Value = allMatches(0).Submatches(0)
End If

这里,
  • Server: - 匹配字符串 Server: + 空格
  • (.*) - 将除换行符以外的零个或多个字符匹配并捕获到第 1 组中,直到行尾。

  • 查看更多关于 capturing groups .

    关于regex - 在 VBA 中使用正则表达式和正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39419234/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com