AHK RegExMatch:从字符串中提取标签

时间:2018-10-22 20:02:29

标签: autohotkey

从字符串中获取“#tag1 keyword1#tag2 keyword2” 我想提取tag1和tag2 我尝试过:

sPat := "O)#([^#\s]*)"  ; return Object array

If RegExMatch(sSearch,sPat,oTag) {
    MsgBox % oTag.Count
    Loop % oTag.Count(){
        tag := oTag[A_Index]
        MsgBox % tag
    }
}

但是它只会找到第一个标签。 (oTag.Count = 1; tag =“ tag1”)

我在做什么错了?

2 个答案:

答案 0 :(得分:0)

RegExMatch仅找到一个匹配项。 “ O”模式在一次匹配中返回子模式。例如,您可以使用以下命令同时提取标记和关键字:

这会在字符串中找到一个正则表达式模式,并从匹配项中解析出两个子模式

sSearch := "#tag1a keyword1 #tag2 keyword2" 
sPat := "O)#(\S+)\s+(\S+)"

if (RegExMatch(string, regex, fields))
  MsgBox % fields[1] "=" fields[2]

输出:

output

您想要的是这样的

这将查找字符串中所有匹配的正则表达式模式

string := "#tag1 keyword1 #tag2 keyword2" 
regex  := "O)#(\S+)\s+(\S+)"
pos := 1

while (pos && RegExMatch(string, regex, fields, pos)) {
  MsgBox % fields[1] "=" fields[2]
  pos := fields.Pos[2] + 1
}

答案 1 :(得分:0)

查看我来到的最终解决方案:

sPat := "#([^#\s]*)" 
sSpace :="%20"
Pos=1
While Pos :=    RegExMatch(sSearch, sPat, tag,Pos+StrLen(tag)) 
    sTags := sTags . sSpace . tag1  

(我在某个AHK论坛中找到了它,但没有写下参考。)

相关问题