2014-01-06 84 views
1

我曾环顾四周,但找不到有我需要的答案。提前道歉,因为我目前正在教自己的正则表达式(在Excel中使用VB),我认为我有一个语法问题。正则表达式在向前查找空格或逗号

我想要什么:
要在文本文档中查找所有5位数字,将它们与日期关联并将其打印到Excel电子表格中。

我得到的是:
该数字的每个集合的每个日期的单个实例。

我认为是错的:
我的正则表达式模式定义。我想找到一个5位数字,可以在数字后面加逗号或空格。

oRegEx.Pattern = "\d{5}(?=([\s]*)|[,])" 

我敢确信,是这里的问题,我也相信这在本质上是语法,但我很新的这我不知道我做错了。我已经在下面发布了我的整个代码。

Public Sub ParseMail() 
    Dim i As Integer 
    Dim x As Integer 

    Dim oFSO As Scripting.FileSystemObject 
    Dim oFile As Scripting.TextStream 
    Dim sHeaderDate As String 
    Dim sIDList As String 
    Dim sTemp As String 
    Dim oRegEx As VBScript_RegExp_55.RegExp 
    Dim oMatches As Object 

    Set oFSO = New Scripting.FileSystemObject 
    Set oFile = oFSO.OpenTextFile("C:\Users\source doc.txt", ForReading) 'Open the exported file. Change path as needed. 
    Set oRegEx = New VBScript_RegExp_55.RegExp 'Instantiate RegEx object 

    oRegEx.IgnoreCase = True 
    oRegEx.Pattern = "\d{5}(?=([\s]*)|[,])" 'Regular expression to identify 5 digit numbers... not working well." 


    i = 1 ' init variable to 1. This is the first row to start writing in spreadsheet. 

    Do While Not oFile.AtEndOfStream ' Read the file until it reaches the end. 
     sTemp = oFile.ReadLine 'Get the first line 
     'Debug.Print sTemp 
     If Left(sTemp, 5) = "Sent:" Then 'Look for the date in the header. 
      sHeaderDate = Mid(sTemp, 7) 'set this variable starting at pos 7 of this line. 
      'Debug.Print sHeaderDate 
     Else 
      'This is not the date header so start checking for IDs. 
      Set oMatches = oRegEx.Execute(sTemp) 
      If Not oMatches Is Nothing Then 'Find anything? 
       If oMatches.Count > 0 Then 
         For x = 0 To oMatches.Count - 1 'walk thru all found values and write to active spreadsheet. 
          ActiveSheet.Cells(i, 1).Value = sHeaderDate 
          ActiveSheet.Cells(i, 2).Value = oMatches(x) 
          i = i + 1 

         Next 
        End If 
       End If 

      End If 
    Loop 

    oFile.Close 

    Set oFile = Nothing 
    Set oFSO = Nothing 
    Set oRegEx = Nothing 

End Sub 
+2

请提供示例文本 – vmg

+0

我不得不编写大部分文本。该文件本身非常大。 –

+0

来自:\t name @ email。COM 发送:\t周四,2013年11月7日2:15 PM 为:\t [email protected] 主题:\t验证需要 - 重要性:\t高 您必须联系包鼻祖将包通过SAFE重新发送给其他用户。 您的套餐尚未发送。 https://www.website.com 如果您没有发送这些文件,请尽快通知[email protected]。 您已经上传了以下文件(S):81575,83288. CT:2 包ID:1953645 该文件将一直持续到2013年11月21日 –

回答

1

对于匹配五位数字后跟一个空间或逗号正则表达式,请尝试:

\d{5}(?=[ ,]) 

,或者如果你真的想要的任何空白字符:

\d{5}(?=[\s,]) 

注意前视空间。 \ s,您使用的将匹配任何空白字符,但这些不仅包括空间

在你的正则表达式,你用

(?=([\s]*)|[,]) 

因此首先要对前瞻出现零次或多次空白字符 - 因为字符可能频繁发生零个时候,你可能不会匹配什么你的期望。

关于你的代码:

oRegEx.IgnoreCase = True 

是无关紧要的,但你需要为了收集所有的比赛添加

oRegEx.Global = True 

+0

这解决了我的语法question-和感谢(真的,谢谢)。然而,我的首要问题仍然是只提取5位数字的第一个“实例”。你有什么建议吗?在我提供的示例中,我只看到81575,而不是83288. –

+0

@ChrisW。在示例文本中,'83288'后面没有逗号或空格。它后面跟着一段时间,所以它不匹配。 –

+0

@BrianStephens当我修改我的代码以包含句点时,我仍然无法在我的打印屏幕(在本例中为Excel)中看到第二个实例,在本例中为83288。我想知道是否没有一个更大的问题导致我的代码导致这种懒惰。我读的所有东西都表明正则表达式很贪婪。 –

1

你的正则表达式来查找所有5位数字(只有5位)会

oRegEx.Pattern = "\b\d{5}\b" 

\b是一个字边界和\d{5}匹配5位

您可以测试这一点here

+0

这个答案与Brian的非常相似,它能100%解决我的语法问题(并且再次感谢你),但是当我使用对模式的建议修改运行我的代码时,我仍然只收到第一个适用的响应。从我的例子中,IE浏览器能够看到81575,但不是83288.对此有何建议?如果我在这里需要帮助,我很抱歉,但是我对于我所做的事情知之甚少,这可能是一个明显的问题,我只是不知道。 –

+0

oMatches.Count会给你一个答案? – SeanC

+0

它只显示1个匹配项。这是令人难以置信的 - 因为当我运行正则表达式而没有我的打印功能时,它会拉2。 –

相关问题