所以我有这个需要导入的逗号分隔文件,但我需要先做一些替换。示例如下所示;
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,,,,end
1 item A,content,content,,,,,,,end
2 item A,content,content,,,,,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,,,,end
1 item B,content,content,,,,,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,,,,end
2 item B 1,content,content,,,,,,,end
2 item B 2,content,content,,,,,,,end
3 item B,content,content,,,,,,,end
每个主项目(例如项目 A)后面都有其子项目(例如 1 个项目 A),这些子项目也可能有子子项目(例如 2 个项目 B 1),如上所示。目标是对主项目进行多重匹配,并将所有匹配放在主项目及其子项目中的特定位置,如下所示;
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,1 a1<br>2 a2<br>3 a3,,,end
1 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
2 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,1 b1<br>2 b2<br>3 b3,,,end
1 item B,content,content,,,,1 b1<br>2 b2<br>3 b3,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 1,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 2,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
3 item B,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
正则表达式(?:^[^,]+,.*?<u>(\d{1,2} .+?)</u>[^,]*,)
确实匹配,但挑战在于如何获取多个匹配项并将它们放置在所有相应主项和子项(以及可用的子子项)中的所需位置。有人能帮忙吗?谢谢。
答案1
最后对初始正则表达式进行了一些调整,以避免过多的回溯。不确定它是否最高效,但我提出的以下 ahk 脚本完成了工作(经过一些实际修改)!任何优化都欢迎。
i:=1, Textds:=[], linen:=0, mainhr:=vList1:=""
FileRead, vList1, C:\Input.txt
loop, parse, vList1, `n,`r`n
{
linen++
Textds[linen] := StrReplace(A_LoopField,"<u>", "¤")
}
linen:=0
While (Textds[i])
{
mainhr:= RegExReplace(RegExReplace(Textds[i],"[^¤]*¤(\d{1,2} .+?)</u>(?=(?:[^,]*,){8})", "$1@"),"@[^@]+$", "@")
if (mainhr)
{
linen:=i, RegExMatch(Textds[i],"^([^,]+),", mainitem)
Loop {
if RegExMatch(Textds[++i],"^(\d{1,2}) [^,]+,¤", subhr)
mainhr:= RegExReplace(mainhr,"^((?:[^@]+@){" subhr1 "})", "$1" RegExReplace(RegExReplace(Textds[i],"[^¤]*¤(\d{1,2} .+?)</u>(?=(?:[^,]*,){8})", "$1ð"),"ð[^ð]+$", "ð"))
RegExMatch(Textds[i],"^\d{1,2} ([^,]+),", subitem)
subitem1:=RegExReplace(subitem1," \d{1,2}$", "")
} Until (subitem1!=mainitem1)
While (linen!=i)
{
mainhr:=RegExReplace(RegexReplace(mainhr, "ð|@", "<br>"),"<br>$", "")
Textds[linen] := StrReplace(Textds[linen],"¤", "<u>")
FileAppend, % RegexReplace(Textds[linen], "(,,,[^,\r\n]*$)", mainhr "$1") "`r`n", C:\Output.txt
linen++
}
}
else
MsgBox, % "Error at line " i
}
MsgBox, Done
return