2016-06-14 419 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