自动热键关键字生成器功能

时间:2018-11-14 19:00:33

标签: regex autohotkey

在这里,我从现有文本中创建新的搜索词。该功能可以理解驼峰式情况。

newSearchWords:= getAutoKeywords(ByRef oldKeywords,ByRef单词)

我不喜欢其中一部分:

if(Match.Value(4))
     keyTemp := Match.Value(4)
        else if(Match.Value(3))
            keyTemp := Match.Value(3)
        else if(Match.Value(2))

这取决于我在此Autohotkey-脚本中在此处使用的正则表达式。 Autohotkey regEx表达式与Perl或某些其他语言并不完全相同。

camelCaseOr:=“(?:[^ A-Z]?)([A-Z] [a-z] +)”
normalOr:=“()([\ W _-] [a-z] +)”
regEx:=“(?:(” camelCaseOr“ |” normalOr“))”
StartingPosition:= 2

我认为可以缩短此正则表达式,这样就不需要整个IF指令。

这取决于性能,因为他以后应该定期处理大量数据。

getAutoKeywords(ByRef oldKeywords, ByRef words){

    newKeyWords := oldKeywords " " words

    firstWord := "" ; backup if founds nothing

    addKeysMAX := 4
    minLength := 4

    camelCaseOr := "(?:[^A-Z]?)([A-Z][a-z]+)"
    normalOr := "()([\W_-][a-z]+)"
    regEx := "(?:(" camelCaseOr "|" normalOr "))"
    StartingPosition  := 2
    addedKeysCounter := 0

    Array := [] ; or Array := Array()
    while(foundPos := RegexMatch( " " newKeyWords, "O)" regEx, Match, StartingPosition - 1 )){
        StartingPosition := Match.Pos(1) + Match.Len(1)

        if(addedKeysCounter >= addKeysMAX)
            break
        preCar1 := Match.Value(2)
        preCar2 := Match.Value(3)
            ;if(preCar1=="|" || preCar2=="|")
            ;   break
        if(Match.Value(4))
            keyTemp := Match.Value(4)
        else if(Match.Value(3))
            keyTemp := Match.Value(3)
        else if(Match.Value(2))
            keyTemp := Match.Value(2)


    s:= ""
        loop,4
            s .= A_Index ":" Match.Value(A_Index) "#"
        ; MsgBox,% s "(" A_LineNumber " " RegExReplace(A_LineFile,".*\\") ")"
        if(0)
            MsgBox,% keyTemp "(" A_LineNumber " " RegExReplace(A_LineFile,".*\\") ")"

        trim_keyTemp := trim(keyTemp," `t`r`n")
        if(!HasVal(Array,trim_keyTemp)){
            if(!firstWord)
                firstWord := trim_keyTemp
            if(minLength <= strlen(trim_keyTemp))
                Array.Push(trim_keyTemp) ; Append this line to the array.
            ArrayCount++
        }

        ; MsgBox,% keyTemp
    }
    newKeyWords := ""
    Loop % ArrayCount
    {
        ; element := Array%A_Index%
        element := Array[A_Index]
        ; MsgBox % "Element number " . A_Index . " is " . Array%A_Index%
        newKeyWords .= element " "
    }
    if(!newKeyWords := rTrim(newKeyWords))
        if(!newKeyWords := firstWord)
            newKeyWords := "without keywords"
        ; MsgBox % "Element number " . A_Index . " is " . Array%A_Index%
    if(1 && InStr(A_ComputerName,"SL5"))
        feedbackMsgBox(RegExReplace(A_LineFile,".*\\") ">" . A_LineNumber, "newKeyWords=" newKeyWords )
    return newKeyWords
}


HasVal(haystack, needle) { ; return index
    if !(IsObject(haystack)) || (haystack.Length() = 0)
        return 0
    for index, value in haystack
        if (value = needle)
            return index
    return 0
}

0 个答案:

没有答案