qq4045728 发表于 2015-5-24 14:49:16

如何提取字符串指定的子串

字符串
<component name="Microsoft-Windows-UnattendedJoin" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <Identification>
                <JoinWorkgroup>WorkGroup</JoinWorkgroup>
            </Identification>
      </component>
    </settings>
    <settings pass="oobeSystem">
      <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <FirstLogonCommands>
                <SynchronousCommand wcm:action="add">
                  <CommandLine>%WinDir%\ES4\Es4.RunTask.exe -FirstLogon</CommandLine>
                  <Description>Es4.FirstLogon</Description>
                  <Order>1</Order>
                </SynchronousCommand>
            </FirstLogonCommands>
            <LogonCommands>
                <AsynchronousCommand wcm:action="add">
                  <CommandLine>%WinDir%\ES4\Es4.RunTask.exe -GoToDesktop</CommandLine>
                  <Order>1</Order>
                  <Description>Es4.GoToDesktop</Description>
                </AsynchronousCommand>
            </LogonCommands>
            <OOBE>
                <SkipMachineOOBE>true</SkipMachineOOBE>
                <SkipUserOOBE>true</SkipUserOOBE>
            </OOBE>
            <RegisteredOrganization>ITianKong.Com</RegisteredOrganization>
            <RegisteredOwner>SkyUser</RegisteredOwner>
            <TimeZone>China Standard Time</TimeZone>
      </component>
    </settings>
</unattend>


要提取
<AsynchronousCommand wcm:action="add">
<CommandLine>%WinDir%\ES4\Es4.RunTask.exe -GoToDesktop</CommandLine>
<Order>1</Order>
<Description>Es4.GoToDesktop</Description>
</AsynchronousCommand>

里面的Es4.RunTask.exe,   但是这个Es4.RunTask.exe 不是固定的,有可能是a.exe也有可能是b.exe

luren666 发表于 2015-5-24 16:39:23

直接查找字符串 <CommandLine> 和 </CommandLine>,然后截取中间的字符串再做相应的处理不就可以了吗?

gto250 发表于 2015-5-24 21:15:43

123.txt中的内容就是你上面的文本
正则不是很懂,做不到一步到位,将就着用吧


$c = FileRead(@ScriptDir & "\123.txt")
$a=StringRegExp($c,"(?is)\Q<CommandLine>\E(.*?)(?=\Q-\E)",2)
$s=StringSplit($a,"\")
msgbox(0,"",$s[$s])

nofindx 发表于 2015-5-24 21:22:17

你的字符串我给存在桌面上的 1.txt 中,或者你也可以认为下列代码中 $String 就是字符串
代码如下:#include <Array.au3>
#include <String.au3>
#include <StringConstants.au3>

$a = _Extract_Str()

Func _Extract_Str()
Local $String = FileRead("C:\Users\wuhy\Desktop\1.txt") , $Str_Return
Local $aArray_syn = _StringBetween($String, "<SynchronousCommand", "</SynchronousCommand>")
Dim $Num = 1
While $Num <= UBound($aArray_syn)
        $Str_Return = UBound($Str_Return)
        Local $aArray_Com = _StringBetween($aArray_syn[$Num - 1],"<CommandLine>","</CommandLine>")
        Local $i_aArray = StringStripWS($aArray_Com,8)
        Local $index = StringInStr($i_aArray,"-")
        Local $ipath = StringLeft($aArray_Com,$index-1)
        Local $str_aArray = StringSplit($ipath,"\")
        Local $sUBound = UBound($str_aArray)
        ReDim $Str_Return[$Num + 1]
        $Str_Return[$Num] = $str_aArray[$sUBound - 1]
        $Num = $Num + 1
WEnd
Return $Str_Return
EndFunc
页: [1]
查看完整版本: 如何提取字符串指定的子串