gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:16:42 27 4
gpt4 key购买 nike

我刚刚使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com