我需要帮助来弄清楚我必须使用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。
我希望你们明白我的意思并且可以帮助我,我会非常感激!
答案 0 :(得分:2)
i)^(?:\*spec\*)?\s*([^:]*)\s+:\s+!hello
我正在使用此正则表达式的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'
----------------------------------------------------------------------