找回密码
 加入
搜索
查看: 4051|回复: 3

[效率算法] 如何提取字符串指定的子串

[复制链接]
发表于 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
发表于 2015-5-24 16:39:23 | 显示全部楼层
直接查找字符串 <CommandLine> 和 </CommandLine>,然后截取中间的字符串再做相应的处理不就可以了吗?
发表于 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[1],"\")
msgbox(0,"",$s[$s[0]])
发表于 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[1]
Local $aArray_syn = _StringBetween($String, "<SynchronousCommand", "</SynchronousCommand>")
Dim $Num = 1
While $Num <= UBound($aArray_syn)
        $Str_Return[0] = UBound($Str_Return)
        Local $aArray_Com = _StringBetween($aArray_syn[$Num - 1],"<CommandLine>","</CommandLine>")
        Local $i_aArray = StringStripWS($aArray_Com[0],8)
        Local $index = StringInStr($i_aArray,"-")
        Local $ipath = StringLeft($aArray_Com[0],$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
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-25 16:17 , Processed in 0.078650 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表