【已解决】正则表达式演示的补充
本帖最后由 chishingchan 于 2023-10-30 19:27 编辑正则表达式演示-『 源码作品 』-AUTOIT CN - Powered by Autoit中文论坛 (autoitx.com)
求助:补充 图1三条结果 和 图2两条结果 的正则表达式,谢谢!
$hGUI = GUICreate("正则表达式演示", 640, 360)
Global $X = 'C:\Users\Administrator\Desktop\desktop.ini'
Global $Y = GUICtrlCreateLabel("$String = 'C:\Users\Administrator\Desktop\desktop.ini'", 10, 10, 360)
GUICtrlCreateLabel("正则表达式", 10, 30)
GUICtrlCreateInput("StringRegExp($String, '^(\w)(?=:)',1)", 10, 50, 356)
GUICtrlCreateInput("StringRegExp($String, '^(\w:)', 1)", 10, 70, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w(:\V+)\\[^\\]+$', 1)", 10, 90, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:(\V+)\\[^\\]+$', 1)", 10, 110, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w(:\V+\\)[^\\]+$', 1)", 10, 130, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:(\V+\\)[^\\]+$', 1)", 10, 150, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w(:\V+)$', 1)", 10, 170, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:(\V+)$', 1)", 10, 190, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:\V+\\([^\\]+)$', 1)", 10, 210, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:\V+\\([^\\]+)\.\w{2,4}$', 1)", 10, 230, 356)
GUICtrlCreateInput("StringRegExp($String, '(?:\.\w{2,4})?$', 1)", 10, 250, 356)
GUICtrlCreateInput("StringRegExp($String, '(?:\.(\w{2,4}))?$', 1)", 10, 270, 356)
GUICtrlCreateInput("", 10, 290, 356)
GUICtrlCreateInput("", 10, 310, 356)
GUICtrlCreateInput("", 10, 330, 356)
$Z = GUICtrlCreateButton("切换", 590, 10, 40, 30)
GUICtrlCreateLabel("返回结果", 376, 30)
Global $A = GUICtrlCreateInput(StringRegExp($X, '^(\w)(?=:)', 1), 376, 50, 254)
Global $B = GUICtrlCreateInput(StringRegExp($X, '^(\w:)', 1), 376, 70, 254)
Global $C = GUICtrlCreateInput(StringRegExp($X, '^\w(:\V+)\\[^\\]+$', 1), 376, 90, 254)
Global $D = GUICtrlCreateInput(StringRegExp($X, '^\w:(\V+)\\[^\\]+$', 1), 376, 110, 254)
Global $E = GUICtrlCreateInput(StringRegExp($X, '^\w(:\V+\\)[^\\]+$', 1), 376, 130, 254)
Global $F = GUICtrlCreateInput(StringRegExp($X, '^\w:(\V+\\)[^\\]+$', 1), 376, 150, 254)
Global $G = GUICtrlCreateInput(StringRegExp($X, '^\w(:\V+)$', 1), 376, 170, 254)
Global $H = GUICtrlCreateInput(StringRegExp($X, '^\w:(\V+)$', 1), 376, 190, 254)
Global $I = GUICtrlCreateInput(StringRegExp($X, '^\w:\V+\\([^\\]+)$', 1), 376, 210, 254)
Global $J = GUICtrlCreateInput(StringRegExp($X, '^\w:\V+\\([^\\]+)\.\w{2,4}$', 1), 376, 230, 254)
Global $K = GUICtrlCreateInput(StringRegExp($X, '(?:\.\w{2,4})?$', 1), 376, 250, 254)
Global $L = GUICtrlCreateInput(StringRegExp($X, '(?:\.(\w{2,4}))?$', 1), 376, 270, 254)
Global $M = GUICtrlCreateInput('C:\Users\Administrator\Desktop', 376, 290, 254)
Global $N = GUICtrlCreateInput('C:\Users\Administrator\Desktop\', 376, 310, 254)
Global $O = GUICtrlCreateInput('C:\Users\Administrator\Desktop\desktop', 376, 330, 254)
GUISetState()
While 1
Switch GUIGetMsg()
Case -3
Exit
Case $Z
If GUICtrlRead($Y) = "$String = 'C:\Users\Administrator\Desktop'" Then
GUICtrlSetData($Y, "$String = 'C:\Users\Administrator\Desktop\desktop.ini'")
$X = "C:\Users\Administrator\Desktop\desktop.ini"
Else
GUICtrlSetData($Y, "$String = 'C:\Users\Administrator\Desktop'")
$X = "C:\Users\Administrator\Desktop"
EndIf
_RegExp()
EndSwitch
WEnd
Func _RegExp()
GUICtrlSetData($A, StringRegExp($X, '^(\w)(?=:)', 1))
GUICtrlSetData($B, StringRegExp($X, '^(\w:)', 1))
GUICtrlSetData($C, StringRegExp($X, '^\w(:\V+)\\[^\\]+$', 1))
GUICtrlSetData($D, StringRegExp($X, '^\w:(\V+)\\[^\\]+$', 1))
GUICtrlSetData($E, StringRegExp($X, '^\w(:\V+\\)[^\\]+$', 1))
GUICtrlSetData($F, StringRegExp($X, '^\w:(\V+\\)[^\\]+$', 1))
GUICtrlSetData($G, StringRegExp($X, '^\w(:\V+)$', 1))
GUICtrlSetData($H, StringRegExp($X, '^\w:(\V+)$', 1))
GUICtrlSetData($I, StringRegExp($X, '^\w:\V+\\([^\\]+)$', 1))
GUICtrlSetData($J, Execute("StringRegExp($X, '^\w:\V+\\([^\\]+)\.\w{2,4}$', 1)"))
GUICtrlSetData($K, StringRegExp($X, '(?:\.\w{2,4})?$', 1))
GUICtrlSetData($L, StringRegExp($X, '(?:\.(\w{2,4}))?$', 1))
GUICtrlSetData($M, 'C:\Users\Administrator')
GUICtrlSetData($N, 'C:\Users\Administrator\')
GUICtrlSetData($O, '')
EndFunc ;==>_RegExp
答案在 4# + 6# StringRegExp($String, '^.+(?=\\)', 1)
StringRegExp($String, '^.+\\', 1)
StringRegExp($String, '^.+(?=\.\w+$)', 1) 1 (.+)\\
2 .+\\
3 (.+)\. afan 发表于 2023-10-30 17:25
StringRegExp($String, '^.+(?=\\)', 1)
StringRegExp($String, '^.+\\', 1)
StringRegExp($String ...
a大,切换后出错,请帮忙看看,谢谢!
$hGUI = GUICreate("正则表达式演示", 640, 360)
Global $X = 'C:\Users\Administrator\Desktop\desktop.ini'
Global $Y = GUICtrlCreateLabel("$String = 'C:\Users\Administrator\Desktop\desktop.ini'", 10, 10, 360)
GUICtrlCreateLabel("正则表达式", 10, 30)
GUICtrlCreateInput("StringRegExp($String, '^(\w)(?=:)',1)", 10, 50, 356)
GUICtrlCreateInput("StringRegExp($String, '^(\w:)', 1)", 10, 70, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w(:\V+)\\[^\\]+$', 1)", 10, 90, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:(\V+)\\[^\\]+$', 1)", 10, 110, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w(:\V+\\)[^\\]+$', 1)", 10, 130, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:(\V+\\)[^\\]+$', 1)", 10, 150, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w(:\V+)$', 1)", 10, 170, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:(\V+)$', 1)", 10, 190, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:\V+\\([^\\]+)$', 1)", 10, 210, 356)
GUICtrlCreateInput("StringRegExp($String, '^\w:\V+\\([^\\]+)\.\w{2,4}$', 1)", 10, 230, 356)
GUICtrlCreateInput("StringRegExp($String, '(?:\.\w{2,4})?$', 1)", 10, 250, 356)
GUICtrlCreateInput("StringRegExp($String, '(?:\.(\w{2,4}))?$', 1)", 10, 270, 356)
GUICtrlCreateInput("StringRegExp($String, '^.+(?=\\)', 1)", 10, 290, 356)
GUICtrlCreateInput("StringRegExp($String, '^.+\\', 1)", 10, 310, 356)
GUICtrlCreateInput("StringRegExp($String, '^.+(?=\.\w+$)', 1)", 10, 330, 356)
$Z = GUICtrlCreateButton("切换", 590, 10, 40, 30)
GUICtrlCreateLabel("返回结果", 376, 30)
Global $A = GUICtrlCreateInput(StringRegExp($X, '^(\w)(?=:)', 1), 376, 50, 254)
Global $B = GUICtrlCreateInput(StringRegExp($X, '^(\w:)', 1), 376, 70, 254)
Global $C = GUICtrlCreateInput(StringRegExp($X, '^\w(:\V+)\\[^\\]+$', 1), 376, 90, 254)
Global $D = GUICtrlCreateInput(StringRegExp($X, '^\w:(\V+)\\[^\\]+$', 1), 376, 110, 254)
Global $E = GUICtrlCreateInput(StringRegExp($X, '^\w(:\V+\\)[^\\]+$', 1), 376, 130, 254)
Global $F = GUICtrlCreateInput(StringRegExp($X, '^\w:(\V+\\)[^\\]+$', 1), 376, 150, 254)
Global $G = GUICtrlCreateInput(StringRegExp($X, '^\w(:\V+)$', 1), 376, 170, 254)
Global $H = GUICtrlCreateInput(StringRegExp($X, '^\w:(\V+)$', 1), 376, 190, 254)
Global $I = GUICtrlCreateInput(StringRegExp($X, '^\w:\V+\\([^\\]+)$', 1), 376, 210, 254)
Global $J = GUICtrlCreateInput(StringRegExp($X, '^\w:\V+\\([^\\]+)\.\w{2,4}$', 1), 376, 230, 254)
Global $K = GUICtrlCreateInput(StringRegExp($X, '(?:\.\w{2,4})?$', 1), 376, 250, 254)
Global $L = GUICtrlCreateInput(StringRegExp($X, '(?:\.(\w{2,4}))?$', 1), 376, 270, 254)
Global $M = GUICtrlCreateInput(StringRegExp($X, '^.+(?=\\)', 1), 376, 290, 254)
Global $N = GUICtrlCreateInput(StringRegExp($X, '^.+\\', 1), 376, 310, 254)
Global $O = GUICtrlCreateInput(StringRegExp($X, '^.+(?=\.\w+$)', 1), 376, 330, 254)
GUISetState()
While 1
Switch GUIGetMsg()
Case -3
Exit
Case $Z
If GUICtrlRead($Y) = "$String = 'C:\Users\Administrator\Desktop'" Then
GUICtrlSetData($Y, "$String = 'C:\Users\Administrator\Desktop\desktop.ini'")
$X = "C:\Users\Administrator\Desktop\desktop.ini"
Else
GUICtrlSetData($Y, "$String = 'C:\Users\Administrator\Desktop'")
$X = "C:\Users\Administrator\Desktop"
EndIf
_RegExp()
EndSwitch
WEnd
Func _RegExp()
GUICtrlSetData($A, StringRegExp($X, '^(\w)(?=:)', 1))
GUICtrlSetData($B, StringRegExp($X, '^(\w:)', 1))
GUICtrlSetData($C, StringRegExp($X, '^\w(:\V+)\\[^\\]+$', 1))
GUICtrlSetData($D, StringRegExp($X, '^\w:(\V+)\\[^\\]+$', 1))
GUICtrlSetData($E, StringRegExp($X, '^\w(:\V+\\)[^\\]+$', 1))
GUICtrlSetData($F, StringRegExp($X, '^\w:(\V+\\)[^\\]+$', 1))
GUICtrlSetData($G, StringRegExp($X, '^\w(:\V+)$', 1))
GUICtrlSetData($H, StringRegExp($X, '^\w:(\V+)$', 1))
GUICtrlSetData($I, StringRegExp($X, '^\w:\V+\\([^\\]+)$', 1))
GUICtrlSetData($J, Execute("StringRegExp($X, '^\w:\V+\\([^\\]+)\.\w{2,4}$', 1)"))
GUICtrlSetData($K, StringRegExp($X, '(?:\.\w{2,4})?$', 1))
GUICtrlSetData($L, StringRegExp($X, '(?:\.(\w{2,4}))?$', 1))
GUICtrlSetData($M, StringRegExp($X, '^.+(?=\\)', 1))
GUICtrlSetData($N, StringRegExp($X, '^.+\\', 1))
GUICtrlSetData($O, StringRegExp($X, '^.+(?=\.\w+$)', 1))
EndFunc ;==>_RegExp
出错行:78 本帖最后由 afan 于 2023-10-30 19:16 编辑
chishingchan 发表于 2023-10-30 18:57
a大,切换后出错,请帮忙看看,谢谢!
最后一行你是要匹配为空?最末加一竖棍子即可'^.+(?=\.\w+$)|'
说实话,直接用 StringRegExp(...) 这样写是不安全的,除非有十足的把握 afan 发表于 2023-10-30 19:10
最后一行你是要匹配为空?最末加一竖棍子即可'^.+(?=\.\w+$)|'
说实话,直接用 StringRegExp(...)...
这所有正则都是之前你给的,如果觉得不妥当请帮忙总结出一份代码,谢谢! afan 发表于 2023-10-30 19:10
最后一行你是要匹配为空?最末加一竖棍子即可'^.+(?=\.\w+$)|'
说实话,直接用 StringRegExp(...)...
反正我对正则一点也不懂!也没时间去学习。:face (24): chishingchan 发表于 2023-10-30 19:18
这所有正则都是之前你给的,如果觉得不妥当请帮忙总结出一份代码,谢谢!
并非针对表达式,而是 StringRegExp(...) 这种书写习惯。通常需要通过 @Error 来判断,而你这里直接以 @Error=0 的前提来取值,这就造成了上面的出错退出。如果确定无误则无所谓~ afan 发表于 2023-10-30 19:22
并非针对表达式,而是 StringRegExp(...) 这种书写习惯。通常需要通过 @Error 来判断,而你这里直接以 ...
a 大,我重复回顾一下旧帖子,找到你给的例子:
GUICreate("正则表达式演示", 690, 360)
Local $aString = ["C:\Users\Administrator\Desktop", "C:\Users\Administrator\Desktop\desktop.ini"]
Local $iIx = 0
Local $String = $aString[$iIx]
Local $iLb = GUICtrlCreateLabel("$String = '" & $String & "'", 10, 10, 400, 18)
GUICtrlCreateLabel("正则表达式", 10, 30)
GUICtrlCreateLabel("返回结果", 430, 30)
$iBtn_Toggle = GUICtrlCreateButton("切换", 640, 10, 40, 30)
Local $aExp = [ _
['(?<=^\w):.+', ''], _
['(?<=^\w:).+', ''], _
['^\w(?=:)|\\[^\\]+$', ''], _
['^\w:|\\[^\\]+$', ''], _
['^\w(?=:)|[^\\]+$', ''], _
['^\w:|[^\\]+$', ''], _
['^\w(?=:)', ''], _
['^\w:', ''], _
['^.+\\', ''], _
['^.+\\|\.\w{2,8}$', ''], _
['^.+?(\.\w{2,8})?$', '\1'], _
['^.+?((?<=\.)\w{2,8})?$', '\1'], _
['^.+(?=\\)', '\1'], _
['^.+\\', '\1'], _
['^.+(?=\.\w+$)', '\1']]
Local $aID
For $ii = 0 To UBound($aExp) - 1
$aID[$ii] = "StringRegExpReplace($String, '" & $aExp[$ii] & "', '" & $aExp[$ii] & "')"
$aID[$ii] = GUICtrlCreateInput($aID[$ii], 10, 50 + 20 * $ii, 410, 18)
$aID[$ii] = GUICtrlCreateInput(Execute($aID[$ii]), 430, 50 + 20 * $ii, 250, 18)
Next
GUISetState()
While 1
Switch GUIGetMsg()
Case -3
Exit
Case $iBtn_Toggle
$iIx = Number(Not $iIx)
$String = $aString[$iIx]
GUICtrlSetData($iLb, "$String = '" & $String & "'")
For $ii = 0 To UBound($aID) - 1
GUICtrlSetData($aID[$ii], Execute($aID[$ii]))
Next
EndSwitch
WEnd
请再帮忙修复一下:24行至26行,谢谢! chishingchan 发表于 2023-10-30 19:40
a 大,我重复回顾一下旧帖子,找到你给的例子:
请再帮忙修复一下:24行至26行,谢谢!
['^.+\K\\.*', ''], _
['^.+\\\K.*', ''], _
['^.+\K\.\w+$|.+', '']]
页:
[1]