Vbscript使用RegExp在两个关键字之间提取数据

时间:2014-08-18 15:06:14

标签: regex vbscript

我想在两个关键字:#START :#Endore

之间提取一个数据块

所以我有一个包含以下内容的文件:

  

XXXXXXXXX

     

YYYYYYYYYYYYY ............

     

:#开始

     

提取数据块...................
  的:#完

     

ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

     

pppppppppppppppppppppppppppppppp

那么如何使用RegExp在vbscript中执行此操作?

2 个答案:

答案 0 :(得分:2)

好的,我解决了这个问题: 所以我解释一下为什么我想提取这样的数据:

所以我有一个包含要从中提取的HTA文件的批处理文件,我将所有HTA代码放在之间:#Start :#Endore

我的批处理文件的一些代码:

@echo off

:#Start
<html>
<head>
<title>Password Entry</title>
<hta:application id="htaid"
applicationName="Password"
border="thin"
icon="wlrmdr.exe"
borderStyle="normal"
caption="yes"
contextMenu="no"
maximizeButton="no"
minimizeButton="yes"
navigable="yes"
showInTaskbar="yes"
singleInstance="yes"
sysmenu="yes"
SCROLL="NO" 
SHOWINTASKBAR="Yes"   
SELECTION="no"
MINIMIZEBUTTON="no" 
>
</head>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES"> 
<BODY TOPMARGIN="1" LEFTMARGIN="1"><CENTER><DIV><SPAN ID="ONSCR"></SPAN></DIV></CENTER></BODY>
<script language="vbscript">
'---------------------------------------------------------------------------------------
Sub Window_OnLoad
    CenterWindow 250,150
    Call PasswordForm()
    Call TextFocus()
end sub
'---------------------------------------------------------------------------------------
Sub CenterWindow(x,y)
    Dim iLeft,itop
    window.resizeTo x,y
    iLeft = window.screen.availWidth/2 - x/2
    itop = window.screen.availHeight/2 - y/2
    window.moveTo ileft,itop
End Sub
'----------------------------------------------------------------------------------------
Sub SaveBatch()
    set fs=CreateObject("Scripting.FileSystemObject")
    strFile=fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2), "UserIn.bat"))
    set ts=fs.OpenTextFile(strFile,2,True)
    If PasswordArea.value <> "" Then
        ts.WriteLine "set PASSWORD="& PasswordArea.value
        ts.Close
        self.Close 'To close this HTA after saving the password as a variable into UserIn.bat
    else
        Msgbox "The password entry is empty ! "& Vbcrlf & "Please type again your passowrd",VbExclamation,"The password entry"
        Location.reload(true) 'To reload this HTA again
    end if
End Sub
'----------------------------------------------------------------------------------------
Sub PasswordForm()
    Self.document.title = "My Password Enrty"
    Self.document.bgColor = "#BBBFFF"
    ONSCR.InnerHTML="<center><FONT COLOR=""#FFFFFF"" SIZE=""+1"" FACE=""VERDANA,ARIAL,HELVETICA,SANS-SERIF"">Password Entry</FONT<br>"_
    &"<input type=""password"" name=""PasswordArea"" size=""20"" onKeyUp=""TextFocus""><P>"_
    &"<input type=""Submit"" STYLE=""HEIGHT:25;WIDTH:110"" value=""OK"" onClick=""SaveBatch"">"
END Sub
'----------------------------------------------------------------------------------------
Sub TextFocus
    PasswordArea.Focus 
End Sub
'----------------------------------------------------------------------------------------
</script>
</body>
</html>
:#End
::***********************************************************************************************
:MyVBS
::'**********************************************************************************************

VBS文件:

 Set fso = CreateObject("Scripting.FileSystemObject")
 Set f=fso.opentextfile("HtaBatchvbscript.bat",1)
 a=f.readall
 Set r=new regexp
 r.Global = True
 r.Multiline = True
 r.IgnoreCase = True
 r.pattern = "(?:^|(?:\r\n))(?::#START\r\n)([\s\S]*?)(?:\r\n)(?::#End)"
 Set Matches = r.Execute(a)
 If Matches.Count > 0 Then Data = Matches(0).SubMatches(0)
 Msgbox Data
 WriteFileText "Test.hta",Data
 f.close
'**********************************************************************************************
Function WriteFileText(sFile,Data)
     Dim objFSO,oTS,sText
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set oTS = objFSO.CreateTextFile(sFile,2)
     oTS.WriteLine Data
     oTS.close
     set oTS = nothing
     Set objFSO = nothing
 End Function 

我刚刚写完这个批处理,生成一个HTA BOX来隐藏CommandLine中的密码,所以我想和你分享:

@echo off
Title Generate a HTA BOX to hide a password in CommandLine Copyright Hackoo 2014
mode con cols=80 lines=5 & color 9B
Set MyVBSFile=%tmp%\%~n0.vbs
Set MyHTAFile=%tmp%\%~n0.hta
Call :CreateMyVBS
Cscript.exe //NOLOGO %MyVBSFile%
start /wait mshta.exe "%MyHTAFile%"
Del "%MyVBSFile%" & Del "%MyHTAFile%"
for /f "tokens=*" %%i in (%tmp%\userIn) do set "Mypassword=%%i"
echo Your password is : %MyPassword%
Del %tmp%\userIn
pause 
:#Start
<html>
<head>
<title>Password Entry</title>
<hta:application id="htaid"
applicationName="Password"
border="thin"
icon="wlrmdr.exe"
borderStyle="normal"
caption="yes"
contextMenu="no"
maximizeButton="no"
minimizeButton="yes"
navigable="yes"
showInTaskbar="yes"
singleInstance="yes"
sysmenu="yes"
SCROLL="NO" 
SHOWINTASKBAR="Yes"   
SELECTION="no"
MINIMIZEBUTTON="no" 
>
</head>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES"> 
<BODY TOPMARGIN="1" LEFTMARGIN="1"><CENTER><DIV><SPAN ID="ONSCR"></SPAN></DIV></CENTER></BODY>
<script language="vbscript">
'---------------------------------------------------------------------------------------
Sub Window_OnLoad
    CenterWindow 250,150
    Call PasswordForm()
    Call TextFocus()
end sub
'---------------------------------------------------------------------------------------
Sub CenterWindow(x,y)
    Dim iLeft,itop
    window.resizeTo x,y
    iLeft = window.screen.availWidth/2 - x/2
    itop = window.screen.availHeight/2 - y/2
    window.moveTo ileft,itop
End Sub
'----------------------------------------------------------------------------------------
Sub SaveBatch()
    set fs=CreateObject("Scripting.FileSystemObject")
    strFile=fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2),"UserIn"))
    set ts=fs.OpenTextFile(strFile,2,True)
    If PasswordArea.value <> "" Then
        ts.WriteLine PasswordArea.value
        ts.Close
        self.Close 'To close this HTA after saving the password as a variable into UserIn.bat
    else
        Msgbox "The password entry is empty ! "& Vbcrlf & "Please type again your passowrd",VbExclamation,"The password entry"
        Location.reload(true) 'To reload this HTA again
    end if
End Sub
'----------------------------------------------------------------------------------------
Sub PasswordForm()
    Self.document.title = "My Password Enrty"
    Self.document.bgColor = "lightblue"
    ONSCR.InnerHTML="<center><FONT COLOR=""#FFFFFF"" SIZE=""+1"" FACE=""VERDANA,ARIAL,HELVETICA,SANS-SERIF"">Password Entry</FONT<br>"_
    &"<input type=""password"" name=""PasswordArea"" size=""20"" onKeyUp=""TextFocus""><P>"_
    &"<input type=""Submit"" STYLE=""HEIGHT:25;WIDTH:110"" value=""OK"" onClick=""SaveBatch"">"
END Sub
'----------------------------------------------------------------------------------------
Sub TextFocus
    PasswordArea.Focus 
End Sub
'----------------------------------------------------------------------------------------
</script>
</body>
</html>
:#End
::***********************************************************************************************
:CreateMyVBS
::'**********************************************************************************************
(
echo. Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo. Set f=fso.opentextfile^("%~f0",1^)
echo. a=f.readall
echo. Set r=new regexp
echo. r.pattern = "(?:^|(?:\r\n))(?::#Start\r\n)([\s\S]*?)(?:\r\n)(?::#End)"
echo. Set Matches = r.Execute^(a^)
echo. If Matches.Count ^> 0 Then Data = Matches^(0^).SubMatches^(0^)
echo. WriteFileText "%MyHTAFile%",Data
echo. f.close
::'**********************************************************************************************
echo. 
echo. Function WriteFileText^(sFile,Data^)
echo.     Dim objFSO,oTS,sText
echo.     Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo.     Set oTS = objFSO.CreateTextFile^(sFile,2^)
echo.     oTS.WriteLine Data
echo.     oTS.close
echo.     set oTS = nothing
echo.     Set objFSO = nothing
echo. End Function 
) > %MyVBSFile%
::'***********************************************************************************************

答案 1 :(得分:0)

假设您的文字位于名为strData ...

的变量中
With New RegExp
    .IgnoreCase = True
    .Pattern = ":#START((.|\n)*?):#End"
    Set Matches = .Execute(strData)
End With

If Matches.Count > 0 Then strText = Matches(0).SubMatches(0)