regex - Autohotkey 使用正则表达式提取文本

标签 regex string variables match autohotkey

我刚刚使用 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\D(\d.*DS3.*)\]"反对 "NW 5xxx DS3 yyy] ,您将在 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.

关于regex - Autohotkey 使用正则表达式提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983063/

相关文章:

python - 一次从多个变量中减去

javascript - 正则表达式替换为 Javascript

正则表达式,冒号分隔列表

java - 将每个数组字符串与 int 进行比较

regex - 用正则表达式替换字符串中第一次出现的 ","

objective-c - 从另一个类实现中获取 CGFloat 实例变量

c++ - 在类范围内声明一个与类属性同名的局部变量

java - 匹配 Java 字符串的正则表达式

java - 如何将有效 .NET 格式的正则表达式转换为有效的 Java 格式?

数组中的字符指针未释放