AHK RegExMatch:从字符串中提取标签
AHK RegExMatch: extract tags from string
来自字符串,例如“#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;标签="tag1")
我做错了什么?
RegExMatch 只找到一个匹配项。一个匹配中的 "O" 模式 returns 子模式。例如,您可以同时提取标签和关键字:
这会在字符串中找到正则表达式模式并从匹配中解析出两个子模式
sSearch := "#tag1a keyword1 #tag2 keyword2"
sPat := "O)#(\S+)\s+(\S+)"
if (RegExMatch(string, regex, fields))
MsgBox % fields[1] "=" fields[2]
输出:
你想要的是这样的:
这会找到字符串中所有匹配的正则表达式模式
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
}
看我得出的最终解决方案:
sPat := "#([^#\s]*)"
sSpace :="%20"
Pos=1
While Pos := RegExMatch(sSearch, sPat, tag,Pos+StrLen(tag))
sTags := sTags . sSpace . tag1
(我在某个AHK论坛上找到的,但没有写下参考。)
来自字符串,例如“#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;标签="tag1")
我做错了什么?
RegExMatch 只找到一个匹配项。一个匹配中的 "O" 模式 returns 子模式。例如,您可以同时提取标签和关键字:
这会在字符串中找到正则表达式模式并从匹配中解析出两个子模式
sSearch := "#tag1a keyword1 #tag2 keyword2"
sPat := "O)#(\S+)\s+(\S+)"
if (RegExMatch(string, regex, fields))
MsgBox % fields[1] "=" fields[2]
输出:
你想要的是这样的:
这会找到字符串中所有匹配的正则表达式模式
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
}
看我得出的最终解决方案:
sPat := "#([^#\s]*)"
sSpace :="%20"
Pos=1
While Pos := RegExMatch(sSearch, sPat, tag,Pos+StrLen(tag))
sTags := sTags . sSpace . tag1
(我在某个AHK论坛上找到的,但没有写下参考。)