【已解决】正则表达式提取盘符及路径
本帖最后由 chishingchan 于 2022-3-13 21:27 编辑原字符串:C:\Users\Administrator\Desktop
求助1:正则表达式提取原字符串为 C:
求助2:正则表达式提取原字符串为 \Users\Administrator\Desktop
谢谢!
--------题外话--------
建议高手另开一个正则表达式的贴子:
文件字符串:$String = "C:\Users\Administrator\Desktop\desktop.ini"
提取 "C" 正则表达式为
提取 "C:" 正则表达式为
提取 ":\Users\Administrator\Desktop" 正则表达式为
提取 "\Users\Administrator\Desktop" 正则表达式为
提取 ":\Users\Administrator\Desktop\" 正则表达式为
提取 "\Users\Administrator\Desktop\" 正则表达式为
提取 ":\Users\Administrator\Desktop\desktop.ini" 正则表达式为
提取 "\Users\Administrator\Desktop\desktop.ini"" 正则表达式为
提取 "desktop.ini" 正则表达式为
提取 "desktop" 正则表达式为
提取 ".ini" 正则表达式为
提取 "ini" 正则表达式为
文件夹字符串:$String = "C:\Users\Administrator\Desktop"
提取 "C" 正则表达式为
提取 "C:" 正则表达式为
提取 ":\Users\Administrator\Desktop" 正则表达式为
提取 "\Users\Administrator\Desktop" 正则表达式为
提取 ":\Users\Administrator" 正则表达式为
提取 "\Users\Administrator" 正则表达式为
提取 ":\Users\Administrator\" 正则表达式为
提取 "\Users\Administrator\" 正则表达式为
Highlight code by AuREHelper
(\w:)(\V+)
一个表达式就行了 本帖最后由 afan 于 2022-3-13 18:13 编辑
文件字符串:$String = "C:\Users\Administrator\Desktop\desktop.ini"
提取 "C" 正则表达式为
Highlight code by AuREHelper
^(\w)(?=:)
提取 "C:" 正则表达式为
Highlight code by AuREHelper
^(\w:)
提取 ":\Users\Administrator\Desktop" 正则表达式为
Highlight code by AuREHelper
^\w(:\V+)\\[^\\](?#)+$
提取 "\Users\Administrator\Desktop" 正则表达式为
Highlight code by AuREHelper
^\w:(\V+)\\[^\\](?#)+$
提取 ":\Users\Administrator\Desktop\" 正则表达式为
Highlight code by AuREHelper
^\w(:\V+\\)[^\\](?#)+$
提取 "\Users\Administrator\Desktop\" 正则表达式为
Highlight code by AuREHelper
^\w:(\V+\\)[^\\](?#)+$
提取 ":\Users\Administrator\Desktop\desktop.ini" 正则表达式为
Highlight code by AuREHelper
^\w(:\V+)$
提取 "\Users\Administrator\Desktop\desktop.ini"" 正则表达式为
Highlight code by AuREHelper
^\w:(\V+)$
提取 "desktop.ini" 正则表达式为
Highlight code by AuREHelper
^\w:\V+\\([^\\](?#)+)$
提取 "desktop" 正则表达式为
Highlight code by AuREHelper
^\w:\V+\\([^\\](?#)+)\.\w{2,4}$
提取 ".ini" 正则表达式为
Highlight code by AuREHelper
\.\w{2,4}$
提取 "ini" 正则表达式为
Highlight code by AuREHelper
\.(\w{2,4})$
这是针对本身是合法路径的,写法很简单,而且也可以有很多种不同写法。
如果不一定是合法路径,那就要做出多种检验,写法也会复杂一些。
另外,这种路径一般用一个表达式就行了,然后自行组合使用,如
Highlight code by AuREHelper
^(\w):\\(\V+)\\([^\\](?#)+)\.(\w{2,4})$
(注:以上有一些(?#)标记,这个本身是用于在表达式做注释用的,不参与表达式。此处的作用只是应对论坛对于html代码的Bug而必须加的,可以忽略)
后面文件夹示例基本一样,去掉 \.\w{2,4} 就行了
a大,打扰了!上面的结果不够直观,结果如下:
我想要的结果如下:
麻烦你改一改,实在看不懂。谢谢!
$hGUI = GUICreate("正则表达式演示",640,320)
$String = "C:\Users\Administrator\Desktop"
GUICtrlCreateLabel("$String = 'C:\Users\Administrator\Desktop'",10,10)
GUICtrlCreateLabel("正则表达式",10,30)
GUICtrlCreateLabel("返回结果",430,30)
GUICtrlCreateInput("StringRegExpReplace($String, '^(\w)(?=:)','')",10,50,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^(\w)(?=:)',''),430,50,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^(\w:)','')",10,70,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^(\w:)',''),430,70,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^\w(:\V+)\\[^\\](?#)+$','')",10,90,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^\w(:\V+)\\[^\\](?#)+$',''),430,90,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^\w:(\V+)\\[^\\](?#)+$','')",10,110,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^\w:(\V+)\\[^\\](?#)+$',''),430,110,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^\w(:\V+\\)[^\\](?#)+$','')",10,130,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^\w(:\V+\\)[^\\](?#)+$',''),430,130,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^\w:(\V+\\)[^\\](?#)+$','')",10,150,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^^\w:(\V+\\)[^\\](?#)+$',''),430,150,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^\w(:\V+)$','')",10,170,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^\w(:\V+)$',''),430,170,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^\w:\V+\\([^\\](?#)+)$','')",10,190,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^\w:\V+\\([^\\](?#)+)$',''),430,190,200)
GUICtrlCreateInput("StringRegExpReplace($String, '^\w:\V+\\([^\\](?#)+)\.\w{2,4}$','')",10,190,410)
GUICtrlCreateInput(StringRegExpReplace($String,'^\w:\V+\\([^\\](?#)+)\.\w{2,4}$',''),430,190,200)
GUICtrlCreateInput("StringRegExpReplace($String, '\.\w{2,4}$','')",10,190,410)
GUICtrlCreateInput(StringRegExpReplace($String,'\.\w{2,4}$',''),430,190,200)
GUICtrlCreateInput("StringRegExpReplace($String, '\.(\w{2,4})$','')",10,190,410)
GUICtrlCreateInput(StringRegExpReplace($String,'\.(\w{2,4})$',''),430,190,200)
GUISetState(@SW_SHOW, $hGUI)
While 1
Switch GUIGetMsg()
Case -3
ExitLoop
EndSwitch
WEnd
afan 发表于 2022-3-13 17:53
文件字符串:$String = "C:%users\Administrator\Desktop\desktop.ini"
提取 "C" 正则表达式为 提取 "C:"...
麻烦看看 4# ,谢谢! chishingchan 发表于 2022-3-13 20:22
麻烦看看 4# ,谢谢!
我上面写的都是匹配方式 StringRegExp,不是替换模式 StringRegExpReplace $hGUI = GUICreate("正则表达式演示", 640, 320)
$String = "C:\Users\Administrator\Desktop"
GUICtrlCreateLabel("$String = 'C:\Users\Administrator\Desktop'", 10, 10)
GUICtrlCreateLabel("正则表达式", 10, 30)
GUICtrlCreateLabel("返回结果", 430, 30)
$sExp = "StringRegExp($String, '^(\w)(?=:)',1)"
GUICtrlCreateInput($sExp, 10, 50, 410)
GUICtrlCreateInput(Execute($sExp), 430, 50, 200)
$sExp = "StringRegExp($String, '^(\w:)', 1)"
GUICtrlCreateInput($sExp, 10, 70, 410)
GUICtrlCreateInput(Execute($sExp), 430, 70, 200)
$sExp = "StringRegExp($String, '^\w(:\V+)\\[^\\](?#)+$', 1)"
GUICtrlCreateInput($sExp, 10, 90, 410)
GUICtrlCreateInput(Execute($sExp), 430, 90, 200)
$sExp = "StringRegExp($String, '^\w:(\V+)\\[^\\](?#)+$', 1)"
GUICtrlCreateInput($sExp, 10, 110, 410)
GUICtrlCreateInput(Execute($sExp), 430, 110, 200)
$sExp = "StringRegExp($String, '^\w(:\V+\\)[^\\](?#)+$', 1)"
GUICtrlCreateInput($sExp, 10, 130, 410)
GUICtrlCreateInput(Execute($sExp), 430, 130, 200)
$sExp = "StringRegExp($String, '^\w:(\V+\\)[^\\](?#)+$', 1)"
GUICtrlCreateInput($sExp, 10, 150, 410)
GUICtrlCreateInput(Execute($sExp), 430, 150, 200)
$sExp = "StringRegExp($String, '^\w(:\V+)$', 1)"
GUICtrlCreateInput($sExp, 10, 170, 410)
GUICtrlCreateInput(Execute($sExp), 430, 170, 200)
$sExp = "StringRegExp($String, '^\w:\V+\\([^\\](?#)+)$', 1)"
GUICtrlCreateInput($sExp, 10, 190, 410)
GUICtrlCreateInput(Execute($sExp), 430, 190, 200)
$sExp = "StringRegExp($String, '^\w:\V+\\([^\\](?#)+)(?:\.\w{2,4})?$', 1)"
GUICtrlCreateInput($sExp, 10, 210, 410)
GUICtrlCreateInput(Execute($sExp), 430, 210, 200)
$sExp = "StringRegExp($String, '(?:\.\w{2,4})?$', 1)"
GUICtrlCreateInput($sExp, 10, 230, 410)
GUICtrlCreateInput(Execute($sExp), 430, 230, 200)
$sExp = "StringRegExp($String, '(?:\.(\w{2,4}))?$', 1)"
GUICtrlCreateInput($sExp, 10, 250, 410)
GUICtrlCreateInput(Execute($sExp), 430, 250, 200)
GUISetState()
While GUIGetMsg() <> -3
WEnd _PathSplit 这函数 中找。
; #FUNCTION# ====================================================================================================================
; Author ........: Valik
; Modified.......: DXRW4E - Re-wrote to use a regular expression; guinness - Update syntax and structure.
; ===============================================================================================================================
Func _PathSplit($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension)
Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
If @error Then ; This error should never happen.
ReDim $aArray
$aArray[$PATH_ORIGINAL] = $sFilePath
EndIf
$sDrive = $aArray[$PATH_DRIVE]
If StringLeft($aArray[$PATH_DIRECTORY], 1) == "/" Then
$sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "\h*[\/\\]+\h*", "\/")
Else
$sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "\h*[\/\\]+\h*", "\\")
EndIf
$aArray[$PATH_DIRECTORY] = $sDir
$sFileName = $aArray[$PATH_FILENAME]
$sExtension = $aArray[$PATH_EXTENSION]
Return $aArray
EndFunc ;==>_PathSplit
本帖最后由 chishingchan 于 2022-3-14 17:06 编辑
绿色风 发表于 2022-3-14 15:07
_PathSplit 这函数 中找。
$PATH_... 这些变量好像没有被 定义
可不可以发个能显示结果的例子?谢谢!
chishingchan 发表于 2022-3-14 17:05
$PATH_... 这些变量好像没有被 定义
可不可以发个能显示结果的例子?谢谢!
帮助文档中的呀,万能的F1 不就出来的了。
指向: file.au3
页:
[1]