首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AHK填充一个Regex匹配数组

AHK填充一个Regex匹配数组
EN

Stack Overflow用户
提问于 2016-05-10 18:39:51
回答 2查看 523关注 0票数 3

在AHK中,我试图以matchi的方式填充一系列匹配。以下是我到目前为止所拥有的:

代码语言:javascript
运行
复制
string = "red"
RegExMatch(string, "O)([a-z])", Match)
MsgBox % Match[1] . Match[2] . Match[3]

然而,它只是返回r而不是红色。

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-10 19:51:37

RegExMatch()中没有“匹配”这样的东西。文件上说

返回最左边出现的 of NeedleRegEx在字符串Hay堆栈中的位置。

以及“模式3(匹配对象)”(这就是您使用的)

匹配对象..。检索的位置、长度和值--总体匹配和每个捕获子模式(如果存在)。

也就是说,子模式仅适用于最左边的匹配。表达式只包含一个子模式:([a-z])

要在同一个字符串中捕获多个匹配(相同表达式),您必须围绕它构建一个循环,并相应地移动StartingPosition参数。

票数 3
EN

Stack Overflow用户

发布于 2022-10-21 08:45:39

以下是我的实现(经过测试,但没有任何bug的保证)

它循环& RegExMatch & shift到下一个位置+>以查找所有匹配的对象。

代码语言:javascript
运行
复制
; you need to use `O)` in your regex to use match object
; https://www.autohotkey.com/boards/viewtopic.php?t=87233
regexMatchAll(content_SearchOn, regexPatternStr_MatchFor, ind_MatchFromPosition := 1) {
  ;~ stdout := FileOpen("*", "w")
  arr_matcher := []
  
  while (true)
  {
    ind_RegexGroupAll := RegExMatch(content_SearchOn, regexPatternStr_MatchFor, matcher, ind_MatchFromPosition)
    ; you cannot use `while (ind_RegexGroupAll != 0)`, cuz its would be evaluated after a Null match already pushed inside the array -> you must `break the loop` soon, before `arr_matcher.Push(matcher)`
    if (ind_RegexGroupAll == 0) { ; Zero is returned if the pattern is not found.
      break
    }
    arr_matcher.Push(matcher)
    
    ;~ ind_RegexGroupAll_Matcher := matcher.Pos ; Dont use this, if no match, all these `matcher.*` are null
    length_RegexGroupAll := matcher.Len
    ind_MatchFromPosition := ind_RegexGroupAll + length_RegexGroupAll
    ;~ stdout.WriteLine("ind_RegexGroupAll: " . ind_RegexGroupAll)
    ;~ stdout.WriteLine("ind_RegexGroupAll_Matcher: " . ind_RegexGroupAll_Matcher)
    ;~ stdout.WriteLine("length_RegexGroupAll: " . length_RegexGroupAll)
    ;~ stdout.WriteLine("ind_MatchFromPosition: " . ind_MatchFromPosition)
    
    if (length_RegexGroupAll == 0) { ; avoid zero-width match
      ind_MatchFromPosition += 1
      if (ind_MatchFromPosition > StrLen(content_SearchOn)) { ; avoid `ind_MatchFromPosition += 1` leads to out of bound -> inf loop (this happens if a zero-width match is at the end of `content_SearchOn`)
        ; @performance `StrLen(content_SearchOn)` can be improved
        break
      }
    }
    
  }
  
  return arr_matcher
}

; ;test; ; >>>>> Run Code Test
; ;test; content_SearchOn =
; ;test; (
; ;test; Mode 1 (default): Specify a variable in which to store the part of Haystack that matched the entire pattern. If the pattern is not found (that is, if the function returns 0), this variable and all array elements below are made blank.
; ;test; 
; ;test; If any capturing subpatterns are present inside NeedleRegEx, their matches are stored in a pseudo-array whose base name is OutputVar. For example, if the variable's name is Match, the substring that matches the first subpattern would be stored in Match1, the second would be stored in Match2, and so on. The exception to this is named subpatterns: they are stored by name instead of number. For example, the substring that matches the named subpattern (?P<Year>\d{4}) would be stored in MatchYear. If a particular subpattern does not match anything (or if the function returns zero), the corresponding variable is made blank.
; ;test; 
; ;test; Within a function, to create a pseudo-array that is global instead of local, declare the base name of the pseudo-array (e.g. Match) as a global variable prior to using it. The converse is true for assume-global functions. However, it is often also necessary to declare each element, due to a common source of confusion.
; ;test; )
; ;test; 
; ;test; regexPatternStr_MatchFor := "m`aO)b(.*?) " ; https://www.autohotkey.com/boards/viewtopic.php?t=50537&p=223767
; ;test; arr_matcher := regexMatchAll(content_SearchOn, regexPatternStr_MatchFor)
; ;test; 
; ;test; stdout := FileOpen("*", "w")
; ;test; for ind_matcher_curr, matcher_curr in arr_matcher 
; ;test; {
; ;test;   stdout.WriteLine(ind_matcher_curr . ": " . matcher_curr.Value)
; ;test; }
; ;test; 
; ;test; ; >>>>> Output
; ;test; ; >"E:\AutoHotkey\AutoHotkey.exe" /ErrorStdOut "C:\Users\Zlgtx\Desktop\AHK scripts\Test\Test.ahk"    
; ;test; ; 1: ble 
; ;test; ; 2: ble 
; ;test; ; 3: below 
; ;test; ; 4: bpatterns 
; ;test; ; 5: base 
; ;test; ; 6: ble's 
; ;test; ; 7: bstring 
; ;test; ; 8: bpattern 
; ;test; ; 9: be 
; ;test; ; 10: be 
; ;test; ; 11: bpatterns: 
; ;test; ; 12: by 
; ;test; ; 13: ber. 
; ;test; ; 14: bstring 
; ;test; ; 15: bpattern 
; ;test; ; 16: be 
; ;test; ; 17: bpattern 
; ;test; ; 18: ble 
; ;test; ; 19: bal 
; ;test; ; 20: base 
; ;test; ; 21: bal 
; ;test; ; 22: ble 
; ;test; ; 23: bal 
; ;test; ; >Exit code: 0    Time: 0.271
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37146465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档