正则表达式提取txt内容到列表出错
如题,需要提取文本文件部分符合条件的到列表,结果始终错误#include <ComboConstants.au3>#include <GUIConstantsEx.au3>
#Include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <ListViewConstants.au3>
$Form_main = GUICreate("", 530, 520, -1, -1)
$Form_main = GUICreate("", 530, 520, -1, -1)
;GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Local $idListview = GUICtrlCreateListView("", 50, 100, 430, 200,-1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
_GUICtrlListView_AddColumn($idListview, "", 40, 2)
_GUICtrlListView_AddColumn($idListview, " 映像文件", 160, 2)
_GUICtrlListView_AddColumn($idListview, "卷号", 40, 2)
_GUICtrlListView_AddColumn($idListview, " 名称", 140, 2)
_GUICtrlListView_AddColumn($idListview, "描述", 50, 2)
_GUICtrlListView_AddColumn($idListview, "系统标志", 50, 2)
$Handle = GUICtrlGetHandle(-1)
$Button1 = GUICtrlCreateButton("添加文件", 208, 402, 73, 33)
GUISetState(@SW_SHOW, $Form_main)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1
wimlibinfo()
EndSwitch
WEnd
Func wimlibinfo()
Local $sMountInfo = @ScriptDir&"\MountInfo.txt"
$MountTxt = FileRead($sMountInfo)
Local $Index = StringRegExp($MountTxt, 'Index:(\V+)', 3)
Local $Filepath = StringRegExp($MountTxt, 'Path: (\V+)', 3)
Local $imagename=StringRegExp($MountTxt, 'Name:(\V+)', 3)
Local $imageDescription=StringRegExp($MountTxt, 'Description: (\V+)', 3)
Local $imageFlags=StringRegExp($MountTxt, 'Flags: (\V+)', 3)
For $i = 1 To UBound($imagename ) - 1
GUICtrlCreateListViewItem(""&"|"&"$Filepath"&"|"&$Index[$i]&"|"&$imagename[$i]&"|"&$imageDescription[$i]&"|"& $imageFlags[$i],$idListview)
Next
EndFunc
MountInfo.txt内容如下:
WIM Information:
----------------
Path: E:\Drv\6he1\install.wim
GUID: 0x2cab3248e5e3264d93bd70b64654caf1
Version: 68864
Image Count: 6
Compression: XPRESS
Chunk Size: 32768 bytes
Part Number: 1/1
Boot Index: 0
Size: 13868234692 bytes
Attributes: Relative path junction
Available Images:
-----------------
Index: 1
Name: Windows 7 SP1 32
Description: Windows 7 SP1 32Internet Explorer 11 USB3.0(intel AMD) 2017-3-30
Directory Count: 12783
File Count: 68201
Total Bytes: 11448213961
Hard Link Bytes: 3672619154
Creation Time: Sat Nov 20 22:26:11 2010 UTC
Last Modification Time: Thu Mar 30 01:46:38 2017 UTC
Architecture: x86
Product Name: Microsoft?Windows?Operating System
Edition ID: Ultimate
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7601
Service Pack Build: 23392
Service Pack Level: 1
Flags: Ultimate
WIMBoot compatible: no
Index: 2
Name: Windows 7 SP1 64
Description: Windows 7 SP1 64Internet Explorer 11 USB3.0(intel AMD) 2017-3-30
Directory Count: 19202
File Count: 96501
Total Bytes: 17544872024
Hard Link Bytes: 5302327613
Creation Time: Sun Nov 21 04:39:25 2010 UTC
Last Modification Time: Thu Mar 30 01:46:38 2017 UTC
Architecture: x86_64
Product Name: Microsoft?Windows?Operating System
Edition ID: Ultimate
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7601
Service Pack Build: 23677
Service Pack Level: 1
Flags: Ultimate
WIMBoot compatible: no
Index: 3
Name: Windows Server 2008 R2 SP1 64
Description: Windows Server 2008 R2 SP1 64 Internet Explorer 11 USB3.0(intel AMD) 2017-3-30
Directory Count: 18491
File Count: 89810
Total Bytes: 14785269175
Hard Link Bytes: 3935563716
Creation Time: Sun Nov 21 04:40:28 2010 UTC
Last Modification Time: Mon Apr 24 09:18:54 2017 UTC
Architecture: x86_64
Product Name: Microsoft?Windows?Operating System
Edition ID: ServerEnterprise
Installation Type: Server
HAL: acpiapic
Product Type: ServerNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7601
Service Pack Build: 23677
Service Pack Level: 1
Flags: ServerEnterprise
WIMBoot compatible: no
Index: 4
Name: Windows Server 2008 R2 SP1 64
Description: Windows Server 2008 R2 SP1 64 Internet Explorer 11 USB3.0(intel AMD) 2017-3-30
Directory Count: 18491
File Count: 89813
Total Bytes: 14850344518
Hard Link Bytes: 3935519467
Creation Time: Sun Nov 21 04:40:28 2010 UTC
Last Modification Time: Thu Mar 30 01:46:39 2017 UTC
Architecture: x86_64
Product Name: Microsoft?Windows?Operating System
Edition ID: ServerDatacenter
Installation Type: Server
HAL: acpiapic
Product Type: ServerNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7601
Service Pack Build: 23677
Service Pack Level: 1
Flags: ServerDataCenter
WIMBoot compatible: no
Index: 5
Name: Windows Server 201664
Description: Windows Server 201664 2017-3-30
Directory Count: 24445
File Count: 149129
Total Bytes: 19600589086
Hard Link Bytes: 7302960887
Creation Time: Wed Dec 14 12:32:25 2016 UTC
Last Modification Time: Fri Mar 31 02:42:47 2017 UTC
Architecture: x86_64
Product Name: Microsoft?Windows?Operating System
Edition ID: ServerDatacenter
Installation Type: Server
HAL: acpiapic
Product Type: ServerNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 10
Minor Version: 0
Build: 14393
Service Pack Build: 0
Service Pack Level: 0
Flags: ServerDataCenter
WIMBoot compatible: no
Index: 6
Name: Windows 10 64
Description: Windows 10 642017-3-30
Directory Count: 23374
File Count: 131630
Total Bytes: 17219991936
Hard Link Bytes: 6483283298
Creation Time: Wed Dec 14 12:29:32 2016 UTC
Last Modification Time: Thu Mar 30 01:46:40 2017 UTC
Architecture: x86_64
Product Name: Microsoft?Windows?Operating System
Edition ID: Enterprise
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 10
Minor Version: 0
Build: 14393
Service Pack Build: 0
Service Pack Level: 0
Flags: Enterprise
WIMBoot compatible: no 42行错了
GUICtrlCreateListViewItem(""&"|"& $Filepath &"|"&$Index[$i]&"|"&$imagename[$i]&"|"&$imageDescription[$i]&"|"& $imageFlags[$i],$idListview)
另外下面这个For $i = 1 To UBound($imagename ) - 1 会导致其他数组超出范围!!!
For $i = 1 To UBound($imagename ) - 1
GUICtrlCreateListViewItem(""&"|"& $Filepath &"|"&$Index[$i]&"|"&$imagename[$i]&"|"&$imageDescription[$i]&"|"& $imageFlags[$i],$idListview)
Next
个人意见仅参考,请看楼下分解!
{:face (411):} 谢谢xzf680,改成二楼的仍然出错For $i = 0 To UBound($imagename ) - 1
GUICtrlCreateListViewItem(""&"|"& $Filepath &"|"&$Index[$i]&"|"&$imagename[$i]&"|"&$imageDescription[$i]&"|"& $imageFlags[$i],$idListview)
Next同时还有一个问题,就是文本文件中有Name: 和Product Name:项,我只需要提取Name: 的值,而StringRegExp($MountTxt, 'Name:(\V+)', 3)会把这两项的值都提取出来,用StringRegExp($MountTxt, '^Name:(\V+)', 3)在正则表达式测试器中能正确提取Name:项的值而不提取Product Name:项的值,但在autoit中就报错,不知是哪点不对?本人刚接触正则表达式,有点蒙。 asdfadf sasdf daf df df asd fasdf 真的额是十块? 本帖最后由 xzf680 于 2017-5-9 07:17 编辑
回复 3# bnwangp
文本文件中有Name: 和Product Name:项,我只需要提取Name: 的值改这条代码为:
For $i = 0 To UBound($imagename) -1 Step 2
另外下面这个For $i = 0 To UBound($imagename) -1 Step 2 照样会导致其他数组超出范围!!!而使源码发生错误
看来这方法写到ListViewItem是行不通的 还是不能成功,哪位大侠能不能帮我改改代码呢?在此深表感谢。用dism获取的wim信息用这种方法就能成功,我想用wimlib来获取wim的信息,但就是不能成功,不知是怎么回事??? 还是不能成功,哪位大侠能不能帮我改改代码呢?在此深表感谢。用dism获取的wim信息用这种方法就能成功,我想用wimlib来获取wim的信息,但就是不能成功,不知是怎么回事??? #include <GuiListView.au3>
$Form_main = GUICreate("", 530, 520, -1, -1)
Local $idListview = GUICtrlCreateListView("", 10, 100, 510, 200);, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
_GUICtrlListView_AddColumn($idListview, " 映像文件", 140, 0)
_GUICtrlListView_AddColumn($idListview, "卷号", 38, 0)
_GUICtrlListView_AddColumn($idListview, " 名称", 180, 0)
_GUICtrlListView_AddColumn($idListview, "描述", 420, 0)
_GUICtrlListView_AddColumn($idListview, "系统标志", 50, 0)
wimlibinfo()
GUISetState(@SW_SHOW, $Form_main)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
EndSwitch
WEnd
Func wimlibinfo()
$MountTxt = FileRead(@ScriptDir & "\MountInfo.txt")
Local $Filepath = StringRegExp($MountTxt, '(?mi)^Path:\h*(\V+)', 1)
If @error Then Return SetError(1)
Local $aData = StringRegExp($MountTxt, '(?mi)^Index:\h*(\d+)\v+Name:\h*(\V+)\v+Description:\h*(\V+)[\s\S]+?^Flags:\h*(\V+)', 3)
If @error Then Return SetError(2)
For $i = 0 To UBound($aData) - 1 Step 4
GUICtrlCreateListViewItem($Filepath & "|" & $aData[$i] & "|" & $aData[$i + 1] & "|" & $aData[$i + 2] & "|" & $aData[$i + 3], $idListview)
Next
EndFunc ;==>wimlibinfo
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#Include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <ListViewConstants.au3>
$Form_main = GUICreate("", 530, 520, -1, -1)
$Form_main = GUICreate("", 530, 520, -1, -1)
;GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Local $idListview = GUICtrlCreateListView("", 50, 100, 430, 200,-1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
_GUICtrlListView_AddColumn($idListview, "", 40, 2)
_GUICtrlListView_AddColumn($idListview, " 映像文件", 160, 2)
_GUICtrlListView_AddColumn($idListview, "卷号", 40, 2)
_GUICtrlListView_AddColumn($idListview, " 名称", 140, 2)
_GUICtrlListView_AddColumn($idListview, "描述", 50, 2)
_GUICtrlListView_AddColumn($idListview, "系统标志", 50, 2)
$Handle = GUICtrlGetHandle(-1)
$Button1 = GUICtrlCreateButton("添加文件", 208, 402, 73, 33)
GUISetState(@SW_SHOW, $Form_main)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1
wimlibinfo()
EndSwitch
WEnd
Func wimlibinfo()
Local $sMountInfo = @ScriptDir&"\MountInfo.txt"
$MountTxt = FileRead($sMountInfo)
Local $Index = StringRegExp($MountTxt, 'Index:(\V+)', 3)
Local $Filepath = StringRegExp($MountTxt, 'Path: (\V+)', 3)
Local $imagename=StringRegExp($MountTxt, '(?m)^Name:(\V+)', 3)
Local $imageDescription=StringRegExp($MountTxt, 'Description: (\V+)', 3)
Local $imageFlags=StringRegExp($MountTxt, 'Flags: (\V+)', 3)
For $i = 0 To UBound($imagename) -1
GUICtrlCreateListViewItem(""&"|"& $Filepath &"|"&$Index[$i+1]&"|"&$imagename[$i]&"|"&$imageDescription[$i]&"|"& $imageFlags[$i],$idListview)
Next
EndFunc 谢谢各位前辈,9楼的是我想要的结果,10楼的虽然没有提取Product Name:项值,但卷2 卷4的Name项也没提取到。当然,在此还是要深表感谢!!!向高手们致敬!!! 好复杂。。。 学习学习。。。
页:
[1]