找回密码
 加入
搜索
查看: 1873|回复: 2

[AU3基础] 正则表达式求助

[复制链接]
发表于 2011-6-15 20:57:52 | 显示全部楼层 |阅读模式
本帖最后由 xulinghai 于 2011-6-16 21:55 编辑

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

test.txt
Disk  Partitions  Cylinders  Heads  Sectors  Mbytes  Model
  1        5        30401     255      63  238475.2  ST3250310AS

Partition  Status   Type     Volume Label   Mbytes   System   Usage
 C:  1        A    PRIMARY                 10252.4  NTFS/HPFS   4%
     2             EXTENDED               228220.3             96%
 D:  3             LOGICAL                 61451.7  NTFS/HPFS  26%
 E:  4             LOGICAL                 81933.0  NTFS/HPFS  34%
 F:  5             LOGICAL                 84835.4  NTFS/HPFS  36%
#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, '([C-Z]:)\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
发表于 2011-6-15 22:54:12 | 显示全部楼层
#include <Array.au3>
Local $Str = _
                'Disk  Partitions  Cylinders  Heads  Sectors  Mbytes  Model' & @CRLF & _
                '  1        5        30401     255      63  238475.2  ST3250310AS' & @CRLF & @CRLF & _
                'Partition  Status   Type     Volume Label   Mbytes   System   Usage' & @CRLF & _
                'C:  1        A    PRIMARY                 10252.4  NTFS/HPFS   4%' & @CRLF & _
                '    2             EXTENDED               228220.3             96%' & @CRLF & _
                'D:  3             LOGICAL                 61451.7  NTFS/HPFS  26%' & @CRLF & _
                'E:  4             LOGICAL                 81933.0  NTFS/HPFS  34%' & @CRLF & _
                'F:  5             LOGICAL                 84835.4  NTFS/HPFS  36%'
MsgBox(0, '原字符串', $Str)
Local $Test = StringRegExp($str, '[A-Z]\:.*?(?:%)', 3)
local $array[1][8],$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][8]
        for $n=0 to 7
                $array[$i][$n]=$temp[$n]
        Next
Next
_ArrayDisplay($array)
 楼主| 发表于 2011-6-15 23:12:18 | 显示全部楼层
本帖最后由 xulinghai 于 2011-6-16 21:59 编辑

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

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

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

C:  1 是一个整体的列,好象也被分开了。想要的效果是按原来的列分配。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-21 04:31 , Processed in 0.099310 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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