2016-06-14 84 views
0

我將很快解釋這一點。AutoHotkey | RegExMatch

我需要幫助來找出我必須使用RegExMatch保存正確的var的什麼樣的字符。

RegExMatch(LLine, "(.*) : !hello", Name) 
    SendInput, y 
    sleep, 1000 
    SendInput, Hello %Name1%, how are you?{enter} 

所以聊天輸出是這樣的,例如:*SPEC* TEST TEST : !hello

我想要的變種是:TEST TEST

(.*)節省的: !hello

前一切我怎樣才能讓他不保存*SPEC*部分?

另外,不是每個人都在他們的名字裏面有那個*SPEC*。當我不在Spec ofc中時,他不會在聊天中顯示它

如果某人的名字只是「TEST」這樣的1個單詞,我希望他將單個單詞保存爲var。


我希望你們明白我的意思,可以幫助我,我會非常感激!

+0

如果要更換字符串與它的regEx部分,使用'regExReplace' – Blauhirn

回答

1

說明
i)^(?:\*spec\*)?\s*([^:]*)\s+:\s+!hello 

Regular expression visualization

我使用insenstitve標誌的情況下對這個表達式,其在AutoHotkey的應用爲i)

AutoHotkey的示例代碼

InputString := "*SPEC* TEST TEST : !hello" 
RegexMatch(InputString, "i)^(?:\*spec\*)?\s*([^:]*)\s+:\s+!hello", Match) 

strMessage := "InputString = '" . InputString . "'" 
strMessage .= "`nName = '" . Match1 . "'" 
MsgBox, % strMessage 

AutoHotkey的輸出

--------------------------- 
DesktopAutomation.ahk 
--------------------------- 
InputString = '*SPEC* TEST TEST : !hello' 
Name = 'TEST TEST' 
--------------------------- 
OK 
--------------------------- 

現場演示

正則表達式例子:https://regex101.com/r/tP1uI5/1

示例文本

*SPEC* TEST TEST : !hello 

樣品匹配

MATCH 1 
1. [7-16] `TEST TEST` 

說明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
i)      set case insensitive mode 
---------------------------------------------------------------------- 
^      the beginning of the string 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    \*      '*' 
---------------------------------------------------------------------- 
    spec      'spec' 
---------------------------------------------------------------------- 
    \*      '*' 
---------------------------------------------------------------------- 
)?      end of grouping 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [^:]*     any character except: ':' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    :      ':' 
---------------------------------------------------------------------- 
    \s+      whitespace (\n, \r, \t, \f, and " ") (1 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    !hello     '!hello' 
---------------------------------------------------------------------- 
+0

非常感謝!這真的幫了我很多! – user3430374