循环遍历 AHK 中的所有正则表达式捕获组

问题描述 投票:0回答:1

我在正则表达式匹配中有一个匹配对象捕获组设置。我正在获取一个以逗号分隔的项目列表,并尝试将其转换为数组。所以我的计划是找到总匹配项,然后循环匹配项并添加引号(不确定是否有更简单的方法来做到这一点)。

我的问题是我不知道找到的匹配项总数是多少。有没有办法检查有多少个值匹配?我有一个名为

max_index

的暂存变量
lab_list_array := []
haystack := "lab.website.int, abc.demo.company.com, product.eu.company.com, 10.199.1.1"
regexmatch(haystack,"O)(([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}|\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})",lab_list)
loop, %max_index%
  {
    lab_list_array := "" lab_list[A_index] "" 
    if [A_index] != %max_index%
      {
        lab_list_array .= ","
      }
  }

请告诉我这是否是构建此数组的错误方法,您会采取不同的方式。

autohotkey
1个回答
0
投票

首先,您可以通过这样做轻松找到数组的最大索引...参见这里

; Returns the length of a linear array.
max_index := lab_list.length() 

但是构建此数组或将字符串转换为数组的更好方法是使用

loop, parse, haystack, CSV

lab_list_array := []
loop, parse, haystack, CSV, " `t" 
  {
    lab_list_array.Push(A_Loopfield)
  }
© www.soinside.com 2019 - 2024. All rights reserved.