AutoHotkey | REGEXMATCH

时间:2016-06-14 13:15:26

标签: character autohotkey

我需要帮助来弄清楚我必须使用RegExMatch保存正确的var所需的字符。

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

所以聊天输出就像这样:*SPEC* TEST TEST : !hello

我希望var为:TEST TEST

(.*)将所有内容保存在: !hello

前面

如何让他不保存*SPEC*部分?

此外,并非所有人都在其名字中*SPEC*。如果我当然不在Spec中,他就不会在聊天中显示它。

如果有人的姓名只有1个单词,如#34; TEST",我希望他将单个单词保存为var。

我希望你们明白我的意思并且可以帮助我,我会非常感激!

1 个答案:

答案 0 :(得分:2)

描述

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'
----------------------------------------------------------------------