在VBScript正则表达式

时间:2018-04-05 11:46:41

标签: regex vbscript

我尝试在另一个文件中的两个字符串之间提取文件的所有行,而没有这些分隔符。 例如:

[General]
Description=Description 

[extractSection]
First Line extracted. It is not an ini section 

Last Line extracted 

[OthersSection]
blablabla

它似乎适用于此脚本。我的第一个vbs之一。

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "E:\Temp\Test.txt"
strTemp = "E:\Temp\Temp.txt"
If objFS.FileExists(strTemp) Then objFS.DeleteFile(strTemp)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
  strLine = objFile.ReadLine
  If isReading = True Then
    If instr(strline,"[") Then
      Set objOutFile = objFS.CreateTextFile(strTemp, True)
      objOutFile.Write(strLine1)
      objOutFile.Close
      Exit Do
    Else
      strline1 = strline1 & strline & vbNewLine
    End If
  Else
    If instr(LCase(strline),"[extractsection]") Then
      isReading = True
    End If 
  End If
Loop
objFile.Close

但它似乎没有得到很好的优化,我的文件高达8Mb 我想尝试使用Regex做同样的事情。我从未使用过,我必须学习 我将此作为开头:\[extractsection\]([\s\S]*?)\[[\s\S]
但我想没有分隔符。

2 个答案:

答案 0 :(得分:0)

谢谢Wiktor。好像(?<=\[extractSection\]\n)(.*(?:\n(?!\[).*)*)让我知道什么是最好的(拉姆|速度)与我的ReadLine脚本在上面,请

答案 1 :(得分:0)

您可以尝试使用此vbscript 而不使用正则表达式

Option Explicit
Dim strFile,strTemp,Full_String,First_Delimiter,Second_Delimiter,Extracted_Data
strFile = "E:\Temp\Test.txt"
strTemp = "E:\Temp\Temp.txt"
Full_String = ReadFileText(strFile)
First_Delimiter = "[extractSection]"
Second_Delimiter = "[OthersSection]"
Extracted_Data = String_Between(Full_String,First_Delimiter,Second_Delimiter)
wscript.echo Extracted_Data
Write2File Extracted_Data,strTemp
'************************************************************************************************
Function String_Between(ByVal Full_String, ByVal First_Delimiter, ByVal Second_Delimiter)
Dim Pos,Pos2
Pos = InStr(Full_String, First_Delimiter)
Pos2 = InStr(Full_String, Second_Delimiter)
If Pos = 0 Or Pos2 = 0 Then
    String_Between = "Missing Delimiter"
    Exit Function
End If
String_Between = Mid(Full_String, Pos + Len(First_Delimiter), Pos2 - (Pos + Len(First_Delimiter)))
End Function
'***********************************************************************************************
Function ReadFileText(sFile)
    Dim objFSO,oTS,sText
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FileExists(sFile) Then
        MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) &_
        " dosen't exists !",VbCritical,"CRITICAL ERROR"
        Wscript.Quit
    Else
        Set oTS = objFSO.OpenTextFile(sFile)
        sText = oTS.ReadAll
        oTS.close
        set oTS = nothing
        Set objFSO = nothing
        ReadFileText = sText
    End if
End Function 
'*********************************************************************************************
Sub Write2File(strText,OutputFile)
    Dim fs,ts 
    Const ForWriting = 2
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.OpenTextFile(OutputFile,ForWriting,True)
    ts.WriteLine strText
    ts.Close
End Sub
'*********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************

这个与RegEx

Option Explicit
Dim strFile,strTemp,Full_String,First_Delimiter,Second_Delimiter,Extracted_Data
strFile = "E:\Temp\Test.txt"
strTemp = "E:\Temp\Temp.txt"
Full_String = ReadFileText(strFile)
First_Delimiter = "[extractSection]"
Second_Delimiter = "[OthersSection]"
Extracted_Data = ExtractData(Full_String,First_Delimiter,Second_Delimiter)
wscript.echo Extracted_Data
Write2File Extracted_Data,strTemp
'***********************************************************************************************
Function ExtractData(Full_String,Start_Delim,End_Delim)
Dim fso,f,r,Matches,Contents,Data
    Start_Delim = Replace(Start_Delim,"[","\[")
    Start_Delim = Replace(Start_Delim,"]","\]")
    End_Delim = Replace(End_Delim,"[","\[")
    End_Delim = Replace(End_Delim,"]","\]")
    Set r=new regexp
    r.pattern = "(?:^|(?:\r\n))(:?"& Start_Delim &"\r\n)([\s\S]*?)(?:\r\n)(?:"& End_Delim &")"
    Set Matches = r.Execute(Full_String)
    If Matches.Count > 0 Then Data = Matches(0).SubMatches(1)
    ExtractData = Data
End Function
'***********************************************************************************************
Function ReadFileText(sFile)
    Dim objFSO,oTS,sText
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FileExists(sFile) Then
        MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) &_
        " dosen't exists !",VbCritical,"CRITICAL ERROR"
        Wscript.Quit
    Else
        Set oTS = objFSO.OpenTextFile(sFile)
        sText = oTS.ReadAll
        oTS.close
        set oTS = nothing
        Set objFSO = nothing
        ReadFileText = sText
    End if
End Function 
'*********************************************************************************************
Sub Write2File(strText,OutputFile)
    Dim fs,ts 
    Const ForWriting = 2
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.OpenTextFile(OutputFile,ForWriting,True)
    ts.WriteLine strText
    ts.Close
End Sub
'*********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************