用于在电子邮件中获取特定单词的Regex vba [关闭] 。

问题描述 投票:0回答:1

我有一个代码,得到一个字符串,并尝试削减在小部分为我得到这个信息,我的代码。

 sText = olItem.Body

     Set Reg1 = CreateObject("VBScript.RegExp")
    ' \s* = invisible spaces
    ' \d* = match digits
    ' \w* = match alphanumeric
    'Reg1.Pattern = "\s"
    With Reg1
        '.Pattern = "(\s*(\w*)\s*(\w*)\s*(\w*)\s*([\d-\.]*))"
        .Pattern = "\S"
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
    End With
    'Debug.Print Reg1.test(sText)
    'MsgBox (Reg1.test(sText))
    If Reg1.test(sText) Then

' each "(\w*)" and the "(\d)" are assigned a vText variable
        Set M1 = Reg1.Execute(sText)

        For Each M In M1
           vText = Trim(M.SubMatches(1))
           'vText2 = Trim(M.SubMatches(2))
           MsgBox (vText)
           'vText3 = Trim(M.SubMatches(3))
           'vText4 = Trim(M.SubMatches(4))
           'vText5 = Trim(M.SubMatches(5))
           'MsgBox (vText3)
        Next
    End If

在Outlook VBA中

我收到这种类型的电子邮件。

Fisrt Name  Guilherme
Last Name   Souza
City        Piracicaba

我想得到这些信息和你们各自的值为例。

Dim FName As string
FName = Guilherme

为了得到它,我正在尝试这种类型的Regex模式。

.Pattern = "First Name"

但它不工作,因为我可以有任何名称和其他一些东西,但我的VBA是错误的,我不能只得到一个或多个。

我有其他的电子邮件,我也想得到,按照Peh的回答,我试了一下。

(Modelo) +(\S+)|(plaqueta) +(\S+)|(Serial Number) +(\S+)|(nome do departamento) +(\S+)|(Destino (estoque ou nome do departamento)) +(\S+)|(Nome do Usuário) +(\S+)|(MAC Address) +(\S+)|(IP do Equipamento) +(\S+)|(MAC Address) +(\S+)|(Número do chamado (se houver)) +(\S+)

enter image description here

对于这个字符串,

Segue movimentação de ativos.

Itens   Equipamento/Computador Retirado Equipamento/Computador  Entregue
Modelo      Lenovo 5020
Número de Ativo (plaqueta)      302235
Serial Number       H000000
Origem (estoque ou nome do departamento)        Estoque
Destino (estoque ou nome do departamento)       Office
Nome do Usuário     Guilherme Souza
MAC Address     0000000000
IP do Equipamento       -
Número do chamado (se houver)   -

我想得到的值例如IP我想得到IP=-和MAC=00000000

excel vba outlook-vba
1个回答
1
投票

你的模式 .Pattern = "\S" 是不利于你寻找的。

如果你的电子邮件中的数据是... ...

First Name  Guilherme
Last Name   Souza
City        Piracicaba

.......那么最好用这样的模式。

(First Name) +(\S+)|(Last Name) +(\S+)|(City) +(\S+)

参见: https:/regex101.comrlHnQoo1。

enter image description here

所以你会得到以下的子匹配。

enter image description here

关于VBA中RexEx的更多信息,请查看: 如何在Microsoft Excel中使用正则表达式(Regex),包括单元格内和循环。

© www.soinside.com 2019 - 2024. All rights reserved.