Regex 如何在从单个文本文件创建多个文本文件时包含标题模式

Regex 如何在从单个文本文件创建多个文本文件时包含标题模式,regex,vbscript,Regex,Vbscript,我有一个名为data.txt的文本文件 W1M0130 03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0 03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1 03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1 03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0 03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0 03/12/2012 00

我有一个名为data.txt的文本文件

W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END
W1M0200
03/12/2012 00:30 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_002 12 11 136 58 0 0 0 1
03/12/2012 00:30 SS_003 3 2 213 91 0 0 0 1
03/12/2012 00:30 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_008 0 0 0 0 0 0 0 0
$END
W1M0230
...
下面是我如何将单个文本文件拆分为多个文本文件的代码:

textFile = "C:\data.txt"
saveTo = "C:\"
writeTo = ""
headingPattern = "(W[0-9][A-Z][0-9]*)"

dim fso,fileFrom,regex
set fso = CreateObject("Scripting.FileSystemObject")
set fileFrom = fso.OpenTextFile(textFile)
set regex = new RegExp

with regex
  .Pattern = headingPattern
  .IgnoreCase = false
  .Global = True
end with

while fileFrom.AtEndOfStream <> true
  line = fileFrom.ReadLine
  set matches = regex.Execute(line)

  if matches.Count > 0 then
    writeTo = saveTo & matches(0).SubMatches(0) & ".txt"
    set fileTo = fso.CreateTextFile(writeTo)
  else
    fileTo.WriteLine(line)
  end if
wend

set fileFrom = nothing
set fso = nothing
set regex = nothing
我设法将单个文本文件拆分为多个文本文件,但在如何包含标题图案线(在示例中)W1M0130方面存在问题

预期结果应为:

W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END

我已经在网上搜索了几个小时,尝试了一些尝试和错误的方法,但仍然没有得到预期的结果。非常感谢你的帮助。谢谢大家!

只需将
文件添加到.WriteLine(匹配(0).子匹配(0))
添加到if,如下所示:

 if matches.Count > 0 then
    writeTo = saveTo & matches(0).SubMatches(0) & ".txt"
    set fileTo = fso.CreateTextFile(writeTo)
    fileTo.WriteLine(matches(0).SubMatches(0))
 else
    fileTo.WriteLine(line)
 end if
您的输出将是:[记事本W1M0130.txt]

W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END

很高兴知道这有帮助:)
W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END