xulinghai 发表于 2011-6-15 20:57:52

正则表达式求助

本帖最后由 xulinghai 于 2011-6-16 21:55 编辑

以前没用正则,最近要用gdisk32显示ListView一个列表,用StringMid()函数截取不准确,因此搜索了正则方面的教程,可惜还是不太明白,论坛中搜到一篇disptw的,与我这个有点相近,但是还不行。

test.txtDiskPartitionsCylindersHeadsSectorsMbytesModel
1      5      30401   255      63238475.2ST3250310AS

PartitionStatus   Type   Volume Label   Mbytes   System   Usage
C:1      A    PRIMARY               10252.4NTFS/HPFS   4%
   2             EXTENDED               228220.3             96%
D:3             LOGICAL               61451.7NTFS/HPFS26%
E:4             LOGICAL               81933.0NTFS/HPFS34%
F:5             LOGICAL               84835.4NTFS/HPFS36%#include <GUIConstantsEx.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)

GUICreate("test", 545, 255)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
$ListView = GUICtrlCreateListView('驱动器|状态|分区类型|卷标|容量(Mb)|文件格式|百分比', 5, 5, 535, 200)
GUISetState(@SW_SHOW)
_Loading()

While 1
        Sleep(1000)
WEnd

Func _Loading()
        Local $str = FileRead(@ScriptDir & '\test.txt'), $i, $Item
        Local $array = StringRegExp($str, '(:)\h+(.+?)\h+(.+?)\h+(.+?)\h+(\d*)\h+(.+?)\h+(\d+)', 3)
       
        If @error Then Return MsgBox(48, '提示', '读取错误')

        For $i = 0 To UBound($array) - 7 Step 7
                $Item = $array[$i] & '|' & $array[$i + 1] & '|' & $array[$i + 2] & '|' & $array[$i + 3] & '|' & $array[$i + 4] & '|' & $array[$i + 5]& '|' & $array[$i + 6]
                GUICtrlCreateListViewItem($Item, $ListView)
        Next
        ;_ArrayDisplay($array, "数组显示")
EndFunc   ;==>_Loading

Func Quit()
        GUIDelete()
        Exit
EndFunc   ;==>Quit

3mile 发表于 2011-6-15 22:54:12

#include <Array.au3>
Local $Str = _
                'DiskPartitionsCylindersHeadsSectorsMbytesModel' & @CRLF & _
                '1      5      30401   255      63238475.2ST3250310AS' & @CRLF & @CRLF & _
                'PartitionStatus   Type   Volume Label   Mbytes   System   Usage' & @CRLF & _
                'C:1      A    PRIMARY               10252.4NTFS/HPFS   4%' & @CRLF & _
                '    2             EXTENDED               228220.3             96%' & @CRLF & _
                'D:3             LOGICAL               61451.7NTFS/HPFS26%' & @CRLF & _
                'E:4             LOGICAL               81933.0NTFS/HPFS34%' & @CRLF & _
                'F:5             LOGICAL               84835.4NTFS/HPFS36%'
MsgBox(0, '原字符串', $Str)
Local $Test = StringRegExp($str, '\:.*?(?:%)', 3)
local $array,$Left,$Right

for $i=0 to UBound($Test)-1       
        $temp=StringRegExp($Test[$i],'[^\h]+',3)
        if UBound($temp)=6 then _ArrayInsert($temp,2,"")
        _ArrayInsert($temp,4,"")
        ReDim $array[$i+1]
        for $n=0 to 7
                $array[$i][$n]=$temp[$n]
        Next
Next
_ArrayDisplay($array)

xulinghai 发表于 2011-6-15 23:12:18

本帖最后由 xulinghai 于 2011-6-16 21:59 编辑

感谢3mile大大,您的代码我慢慢研究下,正则表达式刚接触,以后要多下点功夫这方面了。

=====================================================

搞了半天,还是不行,卷标有可能不是空的,上面代码是插入空的数组.对不是空的卷标不行。

C:1 是一个整体的列,好象也被分开了。想要的效果是按原来的列分配。
页: [1]
查看完整版本: 正则表达式求助