AutoHotkey:正则表达式匹配 Window 具有数十种变体的标题

AutoHotkey: Regex match Window title with dozens of variations

下面是我拥有的脚本的简化版本,用于检查 windows 标题,标题中的数字决定了发送的文本。

我想知道我是否可以合并这些数字的数组而不是使用越来越大的 if/else 语句,但仍然准确地检查 windows 标题。

Loop
{
WinWait, Windows security
WinGetTitle, Title, A
WinGetClass, Class, A
if (RegExMatch(Title, "Windows security: ") AND (Class == "Transparent Windows Client") OR RegExMatch(Title, "Windows security: ") AND (Class == "#32770")) 
{
    if (RegExMatch(Title, "Windows security: (049|064|067|071|077|158|193|210|214|215|221|224|239|... ENHANCE)+.*(POR|R.|WS.)+") AND (Class == "Transparent Windows Client") OR RegExMatch(Title, "Windows security: (049|064|067|071|077|158|193|210|214|215|221|224|239|... ENHANCE)+.*(POR|R.|WS.)+") AND (Class == "#32770"))
        {
        Send ExampleText{Enter}
}
}}
return

假设,您的有效数字数组设置如下

validNumbers := [ "049", "064", "067" ] ; etc..

您可以将该 RegEx 语句分成两步,从而使用数组:

valid:=false
for i,v in validNumbers ; there is no such thing as "if x in [...]" - http://whosebug.com/a/33593563/3779853
    if(inStr(Title,v))
        valid:=true
if(valid) {
    if(RegExMatch(Title, "Windows security: [0-9]+.*(POR|R.|WS.)+")) {
        ; ...
    }
}

当其中一个数字包含在标题中时,这仅与 "any-numbers"-regex 匹配。

另一种方法可能是手动构建大型正则表达式:

regex := "Windows security: ("
for i,v in validNumbers
    regex .= v "|"
regex .= ")+.*(POR|R.|WS.)+"
if(RegExMatch(Title, regex)) {
    ; ...
}