找回密码
 加入
搜索
查看: 4770|回复: 9

[效率算法] 正则表达式该怎么写

  [复制链接]
发表于 2014-10-23 04:23:09 | 显示全部楼层 |阅读模式
\\hywl-hydwb\hywl$ 类似这样的正则式表达式怎么写啊,我想替换掉它不会写,求助
发表于 2014-10-23 10:09:38 | 显示全部楼层
\\\\\w{4}-\w{5}\\\w{4}\$
 楼主| 发表于 2014-10-26 21:36:19 | 显示全部楼层
非常感学 ,我试试先
发表于 2014-10-28 07:53:40 | 显示全部楼层
\\.*?\$
 楼主| 发表于 2014-10-28 22:07:45 | 显示全部楼层
貌似都不行
 楼主| 发表于 2014-10-28 22:08:32 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


#include <array.au3>
#include <file.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("批量文件编码转换、字符替换", 450, 350, 192, 124)
$path_label = GUICtrlCreateLabel("目录", 20, 20, 30, 30)
$path_input = GUICtrlCreateInput("", 60, 16, 250, 25)
$path_button = GUICtrlCreateButton("指定目录..", 320, 15, 80, 25)
$find_label = GUICtrlCreateLabel("查找", 20, 60, 28, 17)
$find_edit = GUICtrlCreateEdit("", 60, 60, 350, 100)
$reg_check = GUICtrlCreateCheckbox("正则查找、替换", 60, 160, 100, 20)
$replace_lable = GUICtrlCreateLabel("换成", 20, 180, 28, 17)
$replace_edit = GUICtrlCreateEdit("", 60, 180, 350, 100)
$to_utf8_button = GUICtrlCreateButton("转成utf-8", 100, 296, 90, 30)
$to_ansi_button = GUICtrlCreateButton("转成ANSI",  200, 296, 90, 30)
$exit_button    = GUICtrlCreateButton("退出",      300, 296, 90, 30)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $exit_button
                        Exit
                Case $path_button
                        $path = FileSelectFolder("遍历目录,包含子目录", "")
                        GUICtrlSetData($path_input,$path)
                Case $to_utf8_button
                        char_convert_replace(266)
                Case $to_ansi_button
                        char_convert_replace(10)


        EndSwitch
WEnd


;批量转换,替换
Func char_convert_replace($codepage)
                        $get_path = GUICtrlRead($path_input)
                        $chk_path=FileExists($get_path)
                        If not $chk_path   Then ;检查路径
                                MsgBox(16,"错误提示","没有选择目录,或者目录不存在!")
                        Else
                                $file_type = InputBox("提示","请输入要转换文件的类型,如:html,txt等文本类型。*表示所有文件。"&@CRLF&"主意:转换后,会删除源文件,请主意转换前备份","*")
                                $array = myFileListToArray_StringReg($get_path,"\."&$file_type&"$")
                                If $array[0] == 0 Then
                                        MsgBox(16,"错误提示","指定目录找不到指定类型的文件")
                                Else
                                        ;循环数组,转换编码,替换字符
                                        For $i=1 To $array[0]
                                                $check_dir = FileGetSize($array[$i])
                                                If $check_dir >0 Then ;排除目录、空文件
                                                        ;读取,删除源文件
                                                        $file=FileOpen($array[$i])
                                                        $get=FileRead($file)
                                                        FileClose($file)
                                                        FileDelete($array[$i])
                                                        ;字符替换
                                                        $find    = GUICtrlRead($find_edit)
                                                        $replace = GUICtrlRead($replace_edit)

                                                        If StringLen($find) > 0 Then  ;如果查找字符存在
                                                                If GUICtrlRead($reg_check) ==1 Then    ;正则替换
                                                                        $get = StringRegExpReplace($get,$find,$replace)
                                                                                                                                                
                                                                                                                                Else
                                                                                                                                                MsgBox(0,"","我在这里")
                                                                        $get = StringReplace($get,$find,$replace)
                                                                EndIf

                                                        EndIf

                                                        ;生成utf8 文件
                                                        $tofile=FileOpen($array[$i],$codepage)
                                                        FileWrite($tofile,$get)
                                                        FileClose($tofile)
                                                EndIf
                                        Next
                                        MsgBox(0,"提示","文件转换完成")
                                EndIf


                        EndIf

EndFunc


;遍历目录,包含子目录
Func myFileListToArray_StringReg($s_dir, $s_filt = ".*?", $i_flag = 0, $b_subdir=True, $s_ex="")
         If StringRight($s_dir,1)="" Then $s_dir = StringTrimRight($s_dir,1)

         If $i_flag<1 Or $i_flag>2 Then $i_flag=0;规范

         Local $i_step = 40000 ;每$i_step个文件重新定义一次数组大小,当文件很多时,此值就发挥作用
         Local $a_return[$i_step+1];初始数组大小,[0]存放共有多少个文件,因为每次一直ReDim很慢的
         Local $i_count = 1;记录找到多少个文件$i_count-1

         Local $a_dir[2];初始数组大小,[0]存放共有多少个文件,因为每次一直ReDim很慢的
         $a_dir[0] = 1;做到第几个数组,代替递归
         $a_dir[1] = $s_dir
         $i_dirCount = 1;共有多少个数组,代替递归用的变量
         Local $a_dir_temp, $isdir, $a_temp

         Local $file, $search

         While $a_dir[0]<UBound($a_dir);代替递归
                 $s_dir = $a_dir[$a_dir[0]]
                 $a_dir_temp = _FileListToArray($s_dir, "*", 0)
                 If UBound($a_dir_temp) <= 1 Then
                         $a_dir[0] += 1
                         ContinueLoop
                 EndIf
                 $i_dirCount = 1
                 For $i = 1 To UBound($a_dir_temp)-1
                         ;$isdir=DirGetSize($s_dir &"" & $a_dir_temp[$i])
                         $isdir = StringInStr(FileGetAttrib($s_dir  &  ""  &  $a_dir_temp[$i]),  "D")
                         If $b_subdir And $isdir>0 Then
                                 _ArrayInsert($a_dir, $a_dir[0]+$i_dirCount, $s_dir &""& $a_dir_temp[$i])
                                 $i_dirCount += 1
                         EndIf
                         Select
                                 Case $i_flag=1;文件
                                         If $isdir>0 Then ContinueLoop
                                 Case $i_flag=2;目录
                                         If $isdir<=0 Then ContinueLoop
                         EndSelect
                         If Not StringRegExp($a_dir_temp[$i], $s_filt, 0) Then ContinueLoop;正则匹配

                         ;这里可加其他条件
                         If $s_ex<>"" And  StringRegExp($a_dir_temp[$i], $s_ex, 0) Then ContinueLoop;排除文件名
                         ;-------------------------

                         ;-----如果每找到一个文件就执行一个自定义函数,将此处改为自定义函数
                         If $i_Count +1 > UBound($a_return)-1 Then ReDim $a_return[UBound($a_return)+$i_step]
                         $a_return[$i_Count] = $s_dir &""& $a_dir_temp[$i]
                         ;--------------------------------------
                         $i_Count += 1
                 Next
                 $a_dir[0] += 1
         WEnd;代替递归结束
         $a_return[0] = $i_count - 1
         ;-----如果每找到一个文件就执行一个自定义函数,考虑返回 $i_count - 1(即多少个符合条件的文件)
         ReDim $a_return[$i_count]
         Return $a_return
         ;---------------------------------------------------------------
EndFunc
 楼主| 发表于 2014-10-28 22:08:54 | 显示全部楼层
这段代码正则式选择替换不了
 楼主| 发表于 2014-10-28 22:08:58 | 显示全部楼层
这段代码正则式选择替换不了
发表于 2014-10-28 23:12:39 | 显示全部楼层
好像还整的挺麻烦,实在懒得看代码~
 楼主| 发表于 2014-10-28 23:16:30 | 显示全部楼层
研究好几天了 ,实在不行了,新手看不懂啊,求帮忙~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-26 07:26 , Processed in 0.080206 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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