我曾环顾四周,但找不到有我需要的答案。提前道歉,因为我目前正在教自己的正则表达式(在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
请提供示例文本 – vmg
我不得不编写大部分文本。该文件本身非常大。 –
来自:\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日 –