Autohotkey 使用正则表达式提取文本
Autohotkey extract text using regex
我刚刚使用 autohotkey 学习正则表达式,但不知道如何提取特定字符串并保存到变量?
我正在搜索的文本行:
T NW CO NORWALK HUB NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC] -1 -1 PSTN
我正在尝试保存,仅 NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC]。
这是我的正则表达式代码:
NW\D\d.DS3.]
但是我如何将其作为变量存储在 autohotkey 中?
我试过 RegexMatch,但它只显示位置。我做错了什么。
您可以提供将保存匹配数组的第三个参数:
RegExMatch(str,"NW\D\d.*DS3.*\]",matches)
然后,matches[0]
将包含匹配项。
如果您在模式内使用捕获组,您将能够通过使用更多索引来访问它们的值。如果你对 "NW 5xxx DS3 yyy]
使用 "NW\D(\d.*DS3.*)\]"
,你将在 matches[0]
中包含整个字符串,而 matches[1]
将包含 5xxx DS3 yyy
.
参见 AHK RegExMatch
文档:
FoundPos := RegExMatch(Haystack, NeedleRegEx [, UnquotedOutputVar = "", StartingPosition = 1])
UnquotedOutputVar
Mode 1 (default): OutputVar is the unquoted name of a variable in which to store the part of Haystack that matched the entire pattern. If the pattern is not found (that is, if the function returns 0), this variable and all array elements below are made blank.
If any capturing subpatterns are present inside NeedleRegEx, their matches are stored in a pseudo-array whose base name is OutputVar. For example, if the variable's name is Match, the substring that matches the first subpattern would be stored in Match1, the second would be stored in Match2, and so on. The exception to this is named subpatterns: they are stored by name instead of number. For example, the substring that matches the named subpattern "(?P<Year>\d{4})"
would be stored in MatchYear. If a particular subpattern does not match anything (or if the function returns zero), the corresponding variable is made blank.
; If you want to delete ALL ....
Only(ByRef C)
{
/*
RegExReplace
https://autohotkey.com/docs/commands/RegExReplace.htm
*/
; NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC]
C:=RegExReplace(C, "NW\s[\w-]+\s\[[\w\s]+\]","",ReplacementCount,-1)
if (ReplacementCount = 0)
return C
else
return Only(C)
} ; Only(ByRef C)
string:="Line of text I am searching: T NW CO NORWALK HUB NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC] -1 -1 PSTN"
Result:=Only(string)
MsgBox, % Result
MsgBox, % Only(string)
我刚刚使用 autohotkey 学习正则表达式,但不知道如何提取特定字符串并保存到变量?
我正在搜索的文本行: T NW CO NORWALK HUB NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC] -1 -1 PSTN
我正在尝试保存,仅 NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC]。
这是我的正则表达式代码: NW\D\d.DS3.]
但是我如何将其作为变量存储在 autohotkey 中?
我试过 RegexMatch,但它只显示位置。我做错了什么。
您可以提供将保存匹配数组的第三个参数:
RegExMatch(str,"NW\D\d.*DS3.*\]",matches)
然后,matches[0]
将包含匹配项。
如果您在模式内使用捕获组,您将能够通过使用更多索引来访问它们的值。如果你对 "NW 5xxx DS3 yyy]
使用 "NW\D(\d.*DS3.*)\]"
,你将在 matches[0]
中包含整个字符串,而 matches[1]
将包含 5xxx DS3 yyy
.
参见 AHK RegExMatch
文档:
FoundPos := RegExMatch(Haystack, NeedleRegEx [, UnquotedOutputVar = "", StartingPosition = 1])
UnquotedOutputVar Mode 1 (default): OutputVar is the unquoted name of a variable in which to store the part of Haystack that matched the entire pattern. If the pattern is not found (that is, if the function returns 0), this variable and all array elements below are made blank.
If any capturing subpatterns are present inside NeedleRegEx, their matches are stored in a pseudo-array whose base name is OutputVar. For example, if the variable's name is Match, the substring that matches the first subpattern would be stored in Match1, the second would be stored in Match2, and so on. The exception to this is named subpatterns: they are stored by name instead of number. For example, the substring that matches the named subpattern"(?P<Year>\d{4})"
would be stored in MatchYear. If a particular subpattern does not match anything (or if the function returns zero), the corresponding variable is made blank.
; If you want to delete ALL ....
Only(ByRef C)
{
/*
RegExReplace
https://autohotkey.com/docs/commands/RegExReplace.htm
*/
; NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC]
C:=RegExReplace(C, "NW\s[\w-]+\s\[[\w\s]+\]","",ReplacementCount,-1)
if (ReplacementCount = 0)
return C
else
return Only(C)
} ; Only(ByRef C)
string:="Line of text I am searching: T NW CO NORWALK HUB NW 201-DS3-WLFRCTAICM5-NRWLCT02K16 [DS3 LEC] -1 -1 PSTN"
Result:=Only(string)
MsgBox, % Result
MsgBox, % Only(string)